ProgrammingHardTCS NQT · Infosys

What is printed?

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

Correct answer: C. ['Alpha', 'alpha', 'beta', 'Beta']

Sorting by the lowercase key groups alpha and beta together. sorted is stable, so Alpha stays before alpha, and beta stays before Beta.

More Python Programming questions

Exams that ask this topic