ProgrammingVery hardProduct-company MCQ pattern

What is printed on the third line?

What is printed on the third line?
print(sorted([(1, 'x'), (1, 'X'), (0, 'z')], key=lambda t: t[1].lower()))
print(sorted(['b', 'A', 'a', 'B']))
print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
  1. A. ['A', 'B', 'a', 'b']
  2. B. ['A', 'a', 'b', 'B']
  3. C. ['b', 'a', 'A', 'B']
  4. D. ['A', 'a', 'c', 'b']

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

str.lower makes 'A' and 'a' equal keys, and the stable sort keeps their input order. The B and b keys also tie, so the original order A, a, b, B is kept.

More Python Programming questions

Exams that ask this topic