ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
data = [('x', 3), ('y', 1), ('z', 3)]
print(max(data, key=lambda t: t[1]), min(data, key=lambda t: -t[1]))
  1. A. ('x', 3) ('x', 3)
  2. B. ('z', 3) ('z', 3)
  3. C. ('x', 3) ('y', 1)
  4. D. ('y', 1) ('x', 3)

Correct answer: A. ('x', 3) ('x', 3)

max and min return the first element that reaches the best key. The first item with the largest value is ('x', 3), and the key -t[1] is smallest first for that same element.

More Python Programming questions

Exams that ask this topic