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])
- ['Bina', 'Chetan', 'Anil']
- ['Bina', 'Anil', 'Chetan']
- ['Anil', 'Chetan', 'Bina']
- ['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
- A two-element slice of a list is replaced by a one-element list. What is…
- The list a is extended once with + and once with += through a second name…
- Even numbers are removed from a list while a for loop is running over it. What…
- Two slices with negative values are taken from a list. What does this print?
- Tuples of different lengths are compared. What does this print?
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