ProgrammingModerateTCS NQT · Accenture

What is printed?

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

Correct answer: D. [('c', 3), ('a', 2), ('b', 2)]

Negating the count sorts higher counts first, and the name breaks ties alphabetically. So c with 3 comes first, then a and b.

More Python Programming questions

Exams that ask this topic