ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
  1. A. ['b', 'B', 'A', 'a']
  2. B. ['A', 'a', 'b', 'B']
  3. C. ['A', 'B', 'a', 'b']
  4. D. ['b', 'a', 'A', 'B']

Correct answer: B. ['A', 'a', 'b', 'B']

The keys are 'a', 'a', 'b', 'b'. Timsort is stable, so among equal keys the original order holds: A before a (positions 1 and 2) and b before B (positions 0 and 3).

More Python Programming questions

Exams that ask this topic