ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from operator import itemgetter
rows = [('b', 2, 'x'), ('a', 2, 'y'), ('c', 1, 'z')]
print([r[0] for r in sorted(rows, key=itemgetter(1, 0))])
  1. A. ['a', 'b', 'c']
  2. B. ['b', 'a', 'c']
  3. C. ['c', 'a', 'b']
  4. D. ['c', 'b', 'a']

Correct answer: C. ['c', 'a', 'b']

itemgetter(1, 0) builds the key (second field, first field). The sorted keys are (1, 'c'), (2, 'a'), (2, 'b'), giving c, a, b.

More Python Programming questions

Exams that ask this topic