The till exports a list of orders, and Maya wants it sorted by price per cup. A named function for a one-line comparison feels like overkill. When does a lambda earn its keep?
A lambda is a small anonymous function. map and filter apply a function to a sequence, but in Python 3 they return lazy iterators. Convert the result to a list when you want to inspect it. A comprehension is often clearer when the operation is simple. You will also see late binding, one-shot iterators, and key functions that replace map-then-sort.
A lambda is a short function
lambda parameters: expression creates a function without a def block. It is useful for a one-line rule passed to another function. If the logic needs a name, several steps, or a docstring, use def instead.
double = lambda n: n * 2
print(double(4))
print((lambda a, b: a + b)(2, 3))Small anonymous functions
map transforms every item
map(function, iterable) calls the function once per item. In Python 3 the result is an iterator, so wrap it in list() to print every value. The original sequence is unchanged.
nums = [1, 2, 3]
print(list(map(lambda n: n + 1, nums)))
print(nums)A lazy map
filter keeps matching items
filter(predicate, iterable) keeps only the items for which the predicate returns True. The predicate can be a lambda or a named function. Convert the result to a list before printing or indexing.
nums = [1, 2, 3, 4]
even = filter(lambda n: n % 2 == 0, nums)
print(list(even))Keep matching values
Late binding in a loop
A lambda looks up free variables when it is called, not when it is created. A loop of lambdas that all reference n will all see the final value. Capture the current value with a default argument when each function needs its own copy.
funcs = [lambda: n for n in range(3)]
print([fn() for fn in funcs])
fixed = [lambda n=n: n for n in range(3)]
print([fn() for fn in fixed])Bind the loop value with a default
map and filter are one-shot
map() and filter() return iterators, not lists. Once consumed, they are exhausted. Convert to a list when you need to inspect or reuse the result; otherwise pass the iterator directly to sum() or any(), which consume it once.
numbers = map(lambda n: n * 2, [1, 2, 3])
print(list(numbers))
print(list(numbers))
filtered = filter(lambda n: n > 1, [1, 2, 3])
print(any(filtered))
print(any(filtered))An exhausted iterator stays empty
Use key instead of map-then-sort
When you need an extreme or an order, pass a key function directly to min, max, or sorted. Building a separate mapped list adds a step and can lose the original record. Keep lambdas to one short expression; use def when the rule needs a name.
orders = [("latte", 4.5), ("tea", 3.0), ("mocha", 5.0)]
cheapest = min(orders, key=lambda item: item[1])
names = sorted(orders, key=lambda item: item[1], reverse=True)
print(cheapest)
print([name for name, price in names])Keep the record, pass the rule
Key points
- lambda creates a small anonymous function.
- map applies a function to every item and returns an iterator.
- filter keeps items whose predicate is true.
- A list comprehension is often easier to read than map or filter.
- Lambdas bind late; iterators consume once; key functions preserve the record.
Common mistakes
- Trying to print a map or filter object and expecting a list.
- Writing a complicated lambda when a named function would be clearer.
- Forgetting that map and filter are lazy until consumed.
- Sorting a mapped list of prices and losing the drink names.
Use map and filter to square the even numbers in [1, 2, 3, 4, 5].
Try it yourself
Change the values, predict the output, then run the code. The Python runtime is downloaded the first time you press Run, so the first run can take a few seconds.
Practice this lesson
12 questions with immediate explanations.