Standard Library · 8 min read · Beginner
Sorting with key=lambda, zip() & any()/all()
Sort lists of dictionaries cleanly with key=lambda, pair sequences with zip(), and express checks clearly with any() and all().
What you'll learn
- Sort objects or dictionaries by any field using sorted(..., key=lambda ...)
- Pair parallel lists safely with zip()
- Write readable boolean checks with any() and all()
Combining sorted(..., key=lambda ...), zip(), and any() / all() turns multi-line loops into crisp, self-documenting Python.
Fill-in-the-Blank Exercise
Sort by Score & Check all()
Sort courses by lessons using a lambda key and check if all scores are >= 80.
ranked = sorted(courses, key= c: c["lessons"], reverse=True)
everyone_passed = (s >= 80 for s in scores)Try It Yourself
Pythonsorted(), zip(), any() & all()
Python Output (WebAssembly / Pyodide)
Click Run Python to execute real Python 3 right in your browser.
Check Your Understanding
Score: 0 / 1
1. What does `all(x > 0 for x in [2, 5, -1])` return?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.