ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
people = [("ann", 30), ("bob", 25), ("cid", 30), ("dan", 25)]
people.sort(key=lambda p: p[0], reverse=True)
people.sort(key=lambda p: p[1])
print([p[0] for p in people])
  1. A. ['dan', 'bob', 'cid', 'ann']
  2. B. ['bob', 'dan', 'ann', 'cid']
  3. C. ['dan', 'bob', 'ann', 'cid']
  4. D. ['bob', 'dan', 'cid', 'ann']

Correct answer: A. ['dan', 'bob', 'cid', 'ann']

Sorting twice with a stable sort gives a multi-key order: the second sort (by age) keeps the reverse-name order from the first sort among equal ages.

More Python Programming questions

Exams that ask this topic