ProgrammingVery hardProduct-company MCQ pattern

What is printed on the second line?

What is printed on the second line?
nums = [5, -3, 0, 2, -3]
print(sorted(nums, key=lambda x: x < 0))
print(sorted(nums, key=lambda x: (x < 0, -x)))
  1. A. [5, 2, 0, -3, -3]
  2. B. [5, 0, 2, -3, -3]
  3. C. [-3, -3, 5, 2, 0]
  4. D. [5, 2, -3, -3, 0]

Correct answer: A. [5, 2, 0, -3, -3]

The tuple key puts non-negatives first and sorts them by descending value, and the two -3 values share a key so they keep input order. The first line only splits by sign and keeps the original order.

More Python Programming questions

Exams that ask this topic