Lambda, map, filter & sorted key functions
Beginnerlambda creates small anonymous functions inline — most powerful as the key= argument to sorted/min/max, and alongside map/filter for quick transformations.
Overview
A lambda is a one-expression function without a name: lambda x: x * 2. Its killer use case in Python is the key= parameter — sorted(), min(), max() accept a function that extracts the comparison value, which replaces Java's Comparator boilerplate entirely. map applies a function to every item, filter keeps items passing a test — though list comprehensions (next chapter) are usually preferred. Interviews constantly require sorting dicts/tuples/objects by custom criteria; key + lambda is that skill.
Sorting by Anything with key=
key receives each element and returns what to sort by. Return a tuple for multi-level sorting. reverse=True flips order. This is THE most-used lambda pattern in real code and interviews.
students = [
{"name": "Asha", "cgpa": 8.7, "year": 3},
{"name": "Vikram", "cgpa": 9.1, "year": 2},
{"name": "Neha", "cgpa": 8.7, "year": 2},
]
# Sort by CGPA descending, then year ascending (tuple key)
ranked = sorted(students, key=lambda s: (-s["cgpa"], s["year"]))
for s in ranked:
print(s["name"], s["cgpa"], s["year"])
# Vikram 9.1 2 / Neha 8.7 2 / Asha 8.7 3
# Top scorer without sorting the whole list
topper = max(students, key=lambda s: s["cgpa"])
# Sort words by length, ties alphabetically
words = ["go", "python", "java", "ai"]
print(sorted(words, key=lambda w: (len(w), w))) # ['ai','go','java','python']map & filter (and when NOT to use lambda)
map/filter return lazy iterators — wrap in list() to see results. If the logic needs statements, multiple lines, or a docstring, use def; assigning a lambda to a name is an anti-pattern (PEP 8).
nums = [1, 2, 3, 4, 5, 6]
doubled = list(map(lambda x: x * 2, nums)) # [2,4,6,8,10,12]
evens = list(filter(lambda x: x % 2 == 0, nums)) # [2,4,6]
# Often cleaner as comprehensions:
doubled = [x * 2 for x in nums]
evens = [x for x in nums if x % 2 == 0]
# map with an existing function — no lambda needed
strs = ["3", "8", "1"]
print(list(map(int, strs))) # [3, 8, 1]
# DON'T: square = lambda x: x*x (use def — PEP 8)
def square(x):
return x * xKey Points to Remember
- 1lambda = single expression, no statements, returns implicitly
- 2key= on sorted/min/max is the highest-value lambda use — tuple keys give multi-level sort
- 3Negate numeric keys (-x) for descending within a tuple key
- 4Prefer comprehensions over map/filter with lambda; prefer def over named lambdas
Interview Questions
Sign in to ask AriaSort a list of dicts by one field descending and another ascending — in one line.
What are the limitations of lambda vs def?
map and filter return iterators in Python 3 — why does that matter?
Ask Aria about Lambda, map, filter & sorted key functions
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.