Lists & Tuples · Question 18 · Hard

Student records are sorted by marks in descending order using a key. In what order are the names printed?

pairs = [("Anil", 82), ("Bina", 91), ("Chetan", 82)]
pairs.sort(key=lambda p: -p[1])
print([p[0] for p in pairs])
  1. ['Bina', 'Chetan', 'Anil']
  2. ['Bina', 'Anil', 'Chetan']
  3. ['Anil', 'Chetan', 'Bina']
  4. ['Anil', 'Bina', 'Chetan']
Answer and explanation

Answer: B. ['Bina', 'Anil', 'Chetan']

Explanation: The key -p[1] puts the highest marks first, so Bina (91) leads. Python's sort is stable: Anil and Chetan have equal keys, so they keep their original order.

Previous: Even numbers are removed from a list while a for loop is running over it. What… · Next: Two slices with negative values are taken from a list. What does this print?

Practise lists & tuples

Try a fresh set of lists & tuples questions with an explanation after every answer, free and without signing up: Lists & Tuples practice. Or sit a full placement mock test.

More lists & tuples questions

More python practice

Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge