Skip to content

Python Comprehensions, Lambdas, and Lazy Iterators

Step 31 of 41

Python Lambda Functions, map, filter, and Sort Keys

Introduction

Python lambda functions provide a short function expression for a transformation, predicate, or sort key. map applies a callable as its iterator is consumed; filter returns the original elements whose predicate result is truthy. Neither returns a reusable list automatically.

This lesson covers objective 5.2 in PCAP-31-03 Comprehensions, Lambdas, Closures, and I/O (22% of the score, 9 of 40 items). You will pass functions as values, trace lazy iterators, sort with compound keys, and recognize callbacks that read a loop variable too late.

Learning objectives

After this lesson you can:

  1. Write a lambda with zero, one or several parameters and default values, and explain that its body is a single expression.
  2. Explain the difference between a lambda and a function defined with def.
  3. Pass a function (named or lambda) as an argument to another function.
  4. Predict the result of map() and filter(), convert it to a list, and explain why it can be read only once.
  5. Use the key parameter of sorted(), min() and max() with lambdas.
  6. Choose between a comprehension, map/filter and a loop.
  7. Avoid the common trap of lambdas created in a loop.

lambda: a small anonymous function

A lambda expression defines a function without a name:

Text
lambda parameters: expression

It returns the value of the expression. There is no return keyword and no statement inside; the body is exactly one expression.

Python
sq = lambda x: x * x
print(sq(4))                          # 16
print((lambda a, b=2: a + b)(1))      # 3
print((lambda *args: len(args))(1, 2, 3))   # 3
print((lambda: 5)())                  # 5
print(type(sq).__name__, sq.__name__) # function <lambda>

A lambda supports the same parameter styles as def: positional parameters, defaults (b=2), *args and keyword arguments. The result of a lambda expression is an ordinary function object; its __name__ is "<lambda>", which makes error messages less helpful than named functions.

A lambda body cannot contain statements such as ordinary assignment, return, for, or try. Expressions are permitted: a comprehension can contain iteration, and a parenthesized assignment expression such as (lambda x: (double := x * 2))(3) returns 6. It can contain a conditional expression, because that is an expression:

Python
print((lambda x: "pos" if x > 0 else "neg")(-1))    # neg
Aspect lambda def
Name anonymous (<lambda>) has a name
Body one expression any number of statements
return implicit explicit
Docstring not possible possible
Typical use short throwaway function as an argument everything else

Assigning a lambda to a name, as in sq = lambda x: x * x, is legal but gains nothing over def sq(x): return x * x, which is easier to debug.

Functions as arguments

Because functions are objects, a function can take another as parameter:

Python
def apply(f, v):
    return f(v)


print(apply(lambda s: s[::-1], "abc"))   # cba
print(apply(abs, -3))                    # 3

apply does not care whether f is a lambda, a def function or a built-in such as abs; it only calls it. Notice that abs is passed without parentheses. Writing apply(abs(), -3) would call abs immediately with no argument and raise TypeError. Passing f means "here is the function"; f() means "call it now". This distinction is a frequent source of errors.

Functions stored, chosen and returned

Since a function is a value, it can be kept in a collection and selected at run time. This replaces long if/elif chains and shows up in realistic code as dispatch tables and processing pipelines.

Python
ops = {"add": lambda a, b: a + b, "mul": lambda a, b: a * b}
print(ops["mul"](3, 4))                  # 12

pipeline = [str.strip, str.lower, lambda s: s.replace(" ", "-")]
v = "  Hello World "
for f in pipeline:
    v = f(v)
print(repr(v))                           # 'hello-world'

In the first example the dictionary maps a name to a function, and ops["mul"](3, 4) has two steps: the subscript fetches the lambda, and the parentheses call it. In the second, each function in the list receives the result of the previous one. Methods of built-in types such as str.strip can be used as ordinary functions: str.strip(s) is equivalent to s.strip(), the same relationship as Lamp.switch(l) and l.switch() in chapter 9.

A function can also return a function:

Python
def scale(f):
    return lambda x: f(x) * 2


double_len = scale(len)
print(double_len("abcd"))                # 8
print(callable(len), callable(5))        # True False

scale(len) returns a new lambda that remembers f (here len). Calling it with "abcd" computes len("abcd") * 2. The mechanism by which the lambda keeps f is the subject of chapter 12. The built-in callable(obj) indicates whether an object supports calling, including functions and classes. It does not prove that a particular argument list is valid. See closure factories and captured variables for the captured variable used by scale.

map(): apply a function to each element

map(function, iterable) calls the function on every element and produces the results.

Python
nums = [5, 2, 9, 4]
print(list(map(lambda x: x * 10, nums)))   # [50, 20, 90, 40]
print(list(map(str.upper, ["a", "b"])))     # ['A', 'B']
print(list(map(len, ["aa", "b"])))          # [2, 1]

Two properties matter for the exam.

map returns an iterator, not a list. It computes values on demand. To see the values you convert it with list(), or you loop over it.

An iterator can be consumed only once.

Python
m = map(lambda x: x + 1, nums)
print(type(m).__name__, list(m), list(m))   # map [6, 3, 10, 5] []

The first list(m) exhausts this iterator. The second finds nothing and returns an empty list. Calls to the mapped function happen during consumption, so its errors can also appear then rather than at map(...) construction.

With several iterables, map supplies one positional argument from each per result. By default it stops at the shortest iterable. Python 3.14 adds strict=True, which raises ValueError on unequal lengths when the iterator is consumed:

Python
print(list(map(lambda a, b: a * b, [1, 2, 3], [4, 5])))   # [4, 10]

filter(): keep the elements that pass a test

filter(function, iterable) keeps the elements for which function(element) is truthy.

Python
print(list(filter(lambda x: x > 3, nums)))              # [5, 9, 4]
print(list(filter(None, [0, 1, "", None, "a", []])))    # [1, 'a']

Passing None as the function keeps the elements that are truthy themselves. Like map, filter returns a one-pass iterator. filter returns selected input objects rather than replacing them with predicate results. A predicate can still mutate a mutable object as a side effect, so prefer a pure test. A common error is to expect filter(lambda x: x * 2, data) to double the values: the lambda is used only as a test, and the original elements are returned.

sorted(), min() and max() with a key

The key argument is a function applied to each element to get the value to compare. The elements themselves are returned, not the keys.

Python
print(sorted(nums, key=lambda x: -x))                         # [9, 5, 4, 2]
print(sorted(["bb", "a", "ccc"], key=len, reverse=True))      # ['ccc', 'bb', 'a']
print(sorted(["b", "A", "c"]))                                # ['A', 'b', 'c']
print(sorted(["b", "A", "c"], key=str.lower))                 # ['A', 'b', 'c']

Default string ordering is lexicographic by Unicode code point. ASCII capitals precede ASCII lowercase letters; this is not a general linguistic collation rule. A key can return a tuple to sort by several criteria; Python compares the tuples element by element.

Python
orders = [("n1", 30, "paid"), ("n2", 12, "open"),
          ("n3", 45, "paid"), ("n4", 12, "paid")]
print(sorted(orders, key=lambda o: (o[1], o[0])))
# [('n2', 12, 'open'), ('n4', 12, 'paid'), ('n1', 30, 'paid'), ('n3', 45, 'paid')]
print(max(orders, key=lambda o: o[1])[0])    # n3
print(min(orders, key=lambda o: o[1])[0])    # n2

The two orders with amount 12 tie on the first element of the key, and the second element (the reference) breaks the tie. sorted is stable: items with equal keys keep their original relative order. min returns the first minimal element it meets.

Comprehension or map and filter?

For these finite lists and pure functions, the styles produce equal values. Compare filtering with Python comprehensions to see the filter position explicitly:

Task Comprehension map/filter
Double each value [x * 2 for x in nums] list(map(lambda x: x * 2, nums))
Keep values above 3 [x for x in nums if x > 3] list(filter(lambda x: x > 3, nums))
Both [x * 2 for x in nums if x > 3] list(map(lambda x: x * 2, filter(lambda x: x > 3, nums)))
Python
print(list(filter(lambda x: x % 2, map(lambda x: x * 3, range(5)))))   # [3, 9]

The inner call first creates a map iterator. Consuming the outer filter then requests each mapped value and tests it in turn, keeping 3 and 9. This is a lazy pipeline, not two eagerly built intermediate lists. A comprehension is usually shorter and clearer, and it returns a list immediately. map with a named function (map(len, words)) is concise and fine. When the logic needs more than a short expression, write a named function with def and pass its name.

A trap: lambdas created in a loop

A lambda reads a variable from the enclosing scope when it is called, not when it is created. This is developed fully in chapter 12 (closures); here you only need to recognise the symptom.

Python
fs = [lambda x: x + k for k in range(3)]
print([f(10) for f in fs])           # [12, 12, 12]

fs = [lambda x, k=k: x + k for k in range(3)]
print([f(10) for f in fs])           # [10, 11, 12]

In the first list, all three lambdas look up k after the comprehension has finished, when k is 2. In the second, k=k is a default value evaluated at creation time, which freezes each value.

Worked example: a sorted and filtered order summary

Statement. Using the orders list above (reference, amount, status), compute the references of paid orders, the total of paid amounts, and the paid orders sorted by amount from highest to lowest. Predict the output of:

Python
paid = list(filter(lambda o: o[2] == "paid", orders))
print([o[0] for o in paid])
print(sum(map(lambda o: o[1], paid)))
print([o[0] for o in sorted(paid, key=lambda o: o[1], reverse=True)])

Step 1: paid. The test keeps the orders whose status is "paid": n1 (30), n3 (45), n4 (12). n2 is open and is removed. list(...) stores the result so it can be used three times; without it the iterator would be consumed by the first use.

Step 2: the references. The comprehension takes the first element of each remaining tuple, in the original order: ['n1', 'n3', 'n4'].

Step 3: the total. map extracts the amounts 30, 45, 12; sum adds them: 87.

Step 4: the sorted list. key returns the amount and reverse=True orders from the highest: 45, 30, 12, hence ['n3', 'n1', 'n4'].

Text
['n1', 'n3', 'n4']
87
['n3', 'n1', 'n4']

Why other readings fail. Including n2 in the total (99 instead of 87) forgets the filter. If paid were left as the iterator returned by filter, the second line would print 0 after the first line consumed it. Using reverse=True on the references instead of the amounts would give ['n4', 'n3', 'n1'].

Common pitfalls and exam tips

Pitfall What happens Remedy
Printing map(...) directly Shows <map object at ...> Convert with list()
Reusing a map or filter result Second use is empty Store list(...) once
filter(lambda x: x * 2, data) expecting doubled values Elements are only selected Use map to transform
Passing f() instead of f Calls the function at once Pass the name without parentheses
Statements in a lambda (lambda x: return x) SyntaxError Use def
map with unequal lengths Stops at the shortest by default; strict mode in Python 3.14 raises Choose the intended length policy
Reading a key function as a value to return sorted returns the elements, not the keys Trace with a table of key and element
Lambdas created in a loop All see the final value Use a default argument or a factory (chapter 12)

Exam tip. Distinguish construction from consumption. For a lazy pipeline, trace one requested input through each function before moving to the next.

Exam tip. sorted returns a new list and leaves the source unchanged; list.sort() sorts in place and returns None.

Exam tip. map passes one argument per input iterable. Compare that count with the callable’s accepted positional arguments, including defaults and variadic parameters.

Exam tip. (lambda x: x + 1)(4) calls a lambda immediately and gives 5; lambda x: x + 1 alone is just a function object, and printing it shows <function <lambda> at ...>. Check whether the parentheses with an argument follow the lambda.

End-of-lesson case study

Situation. A shop needs the three cheapest products under 50, by name, with ties resolved alphabetically. A junior developer wrote the code below. Predict the output and find any problem.

Python
products = [("pen", 3), ("lamp", 45), ("desk", 120), ("cup", 3), ("book", 12)]

cheap = filter(lambda p: p[1] < 50, products)
ordered = sorted(cheap, key=lambda p: (p[1], p[0]))
names = list(map(lambda p: p[0], ordered))[:3]
print(names)
print(list(cheap))

Solution, commented. filter keeps pen, lamp, cup and book; desk costs 120 and is removed. sorted consumes the filter iterator completely and orders by price and then name: cup (3) and pen (3) tie on price, so cup comes first; then book (12), then lamp (45). map extracts the names, and the slice keeps the first three: ['cup', 'pen', 'book'].

The second print shows the problem: cheap is an iterator that sorted has already exhausted, so list(cheap) is []. If the program needed cheap again, it should be stored with cheap = list(filter(...)). A cleaner solution avoids the lambdas for the simple extraction: [name for name, price in sorted(products, key=lambda p: (p[1], p[0])) if price < 50][:3] gives the same names, because filtering before or after sorting does not change the relative order of the items kept.

Key terms

  • Lambda: an anonymous function whose body is a single expression.
  • Higher-order function: a function that takes or returns another function.
  • map(f, it): lazily applies f to each element.
  • filter(f, it): lazily keeps elements for which f is truthy.
  • Iterator exhaustion: after one full pass, the iterator yields nothing.
  • key function: computes the value used to compare elements when sorting or choosing a minimum or maximum.

Summary checklist

  • I can write and call a lambda with several parameters and defaults.
  • I know a lambda body is one expression, with no statements.
  • I can predict map and filter results and convert them with list().
  • I know map and filter iterators can be read only once.
  • I can explain what filter keeps and what map produces.
  • I can trace sorted(..., key=...), including ties and reverse=True.
  • I can translate between comprehensions and map/filter.
  • I recognise the lambda-in-a-loop trap and the default-argument fix.

Official sources