ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(sorted(['bb', 'a', 'cc', 'd', 'ee'], key=len, reverse=True))
  1. A. ['bb', 'cc', 'ee', 'a', 'd']
  2. B. ['ee', 'cc', 'bb', 'd', 'a']
  3. C. ['bb', 'cc', 'ee', 'd', 'a']
  4. D. ['cc', 'bb', 'ee', 'a', 'd']

Correct answer: A. ['bb', 'cc', 'ee', 'a', 'd']

reverse=True keeps equal keys in their original relative order (sort is stable in both directions). Reversing a sorted list instead would flip the ties to ['ee', 'cc', 'bb', 'd', 'a'].

More Python Programming questions

Exams that ask this topic