ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: B. ['bob', 'dan', 'ann', 'cy']

The first sort orders names alphabetically. The second sort is stable, and with reverse=True equal ages keep the order from the first pass, so bob comes before dan and ann before cy.

More Python Programming questions

Exams that ask this topic