ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(sorted([('b', 1), ('a', 1), ('c', 2)], key=lambda t: t[1], reverse=True))
  1. A. [('a', 1), ('b', 1), ('c', 2)]
  2. B. [('c', 2), ('b', 1), ('a', 1)]
  3. C. [('c', 2), ('a', 1), ('b', 1)]
  4. D. [('b', 1), ('a', 1), ('c', 2)]

Correct answer: B. [('c', 2), ('b', 1), ('a', 1)]

reverse=True does not reverse equal elements: sorted keeps ties in their original order, so b comes before a. Only the sort order of the keys is reversed.

More Python Programming questions

Exams that ask this topic