ProgrammingHardTCS NQT · Wipro pattern

What is printed?

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

Correct answer: B. [[1, 'z'], [2, 'a'], [2, 'b']]

Lists compare element by element, so the smallest first item is 1. Among the two items starting with 2, the letters decide the order.

More Python Programming questions

Exams that ask this topic