Predict Lambda with sorted
Predict Output Medium
Question
What does this code print?
Code
people = [("Alice", 30), ("Bob", 25), ("Carol", 35)]
sorted_people = sorted(people, key=lambda p: p[1])
print(sorted_people[0][0])
Hint
The lambda sorts by the second element (age).
Explanation
sorted() uses the lambda key=lambda p: p[1] to sort by age. Sorted by age: Bob (25), Alice (30), Carol (35). sorted_people[0] is ('Bob', 25), so [0][0] is 'Bob'. Output: Bob
Related Resources
Related Lessons
- Lambda Functions
Lambda as sort key.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.