ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
print(sorted([(2, 'b'), (1, 'z'), (2, 'a')], reverse=True))
  1. A. [(2, 'b'), (2, 'a'), (1, 'z')]
  2. B. [(2, 'a'), (2, 'b'), (1, 'z')]
  3. C. [(1, 'z'), (2, 'a'), (2, 'b')]
  4. D. [(2, 'b'), (1, 'z'), (2, 'a')]

Correct answer: A. [(2, 'b'), (2, 'a'), (1, 'z')]

Tuples compare the first element and then the second. In reverse order, the (2, 'b') tuple comes first, then (2, 'a'), then (1, 'z').

More Python Programming questions

Exams that ask this topic