Lambdas and Higher-Order Functions

What you'll learn
- That functions are values, and what a higher-order function is
- How to write small anonymous functions with
lambda - How to sort, find
min/max, and group with customkeyfunctions - How
map()andfilter()compare with comprehensions - How to create functions that build other functions
Why it matters
You've already written sorted(names, key=str.lower): you handed a function to another function, and it used it for you. Instead of writing a new sorting loop for every situation, you write one small function that says what to compare, and reuse Python's fast, tested sorting. The same idea powers event handlers and data pipelines.
Functions are values
A function name refers to a function object, just like a variable name refers to a number or string. You can assign it to another name, put it in a list, or pass it around:
def shout(text):
return text.upper() + "!"
def whisper(text):
return text.lower() + "..."
speak = shout # no parentheses: the function itself
print(speak("hello"))
for style in [shout, whisper]:
print(style("Good morning"))HELLO!
GOOD MORNING!
good morning...Functions can even be values in a dictionary, which is a neat replacement for a long if/elif chain:
def add(a, b):
return a + b
def subtract(a, b):
return a - b
operations = {"+": add, "-": subtract}
print(operations["+"](10, 4))
print(operations["-"](10, 4))14
6Higher-order functions
A higher-order function is a function that takes another function as an argument, or returns a function. Here's one you can write yourself:
def apply_to_all(func, items):
"""Return a new list with func applied to every item."""
return [func(item) for item in items]
def double(n):
return n * 2
print(apply_to_all(double, [1, 2, 3]))
print(apply_to_all(str.upper, ["a", "b"]))[2, 4, 6]
['A', 'B']apply_to_all doesn't care what func does; it just calls it.
lambda: tiny anonymous functions
Defining a whole function just to pass double somewhere feels heavy. A lambda creates a small function in a single expression:
lambda parameters: expressiondouble = lambda n: n * 2 # the same as def double(n): return n * 2
print(double(21))
add = lambda a, b: a + b
print(add(3, 4))42
7Rules of a lambda:
- It can contain only one expression, whose value is returned automatically. No
returnkeyword, and no statements likeifblocks, loops, or assignments. - It has no name of its own, which is why it's called anonymous.
Assigning a lambda to a name, as above, is legal but discouraged by PEP 8; the examples above do it only to compare with def. If it deserves a name, use def. Lambdas are meant to be written directly where they're used, as arguments:
print(apply_to_all(lambda n: n ** 2, [1, 2, 3]))[1, 4, 9]A conditional expression works inside a lambda, written right where it's used. Here map(), covered below, applies the lambda to each score:
print(list(map(lambda score: "pass" if score >= 60 else "fail", [75, 40])))['pass', 'fail']Sorting with key
This is where you'll use lambdas the most. The key function receives one item and returns the value to sort by:
students = [
{"name": "Aziza", "age": 20, "score": 92},
{"name": "Bekzod", "age": 22, "score": 85},
{"name": "Dilnoza", "age": 19, "score": 97},
]
by_age = sorted(students, key=lambda s: s["age"])
by_score = sorted(students, key=lambda s: s["score"], reverse=True)
print([s["name"] for s in by_age])
print([s["name"] for s in by_score])['Dilnoza', 'Aziza', 'Bekzod']
['Dilnoza', 'Aziza', 'Bekzod']More key examples:
products = [("Tea", 15000), ("Coffee", 45000), ("Juice", 12000)]
words = ["banana", "Apple", "cherry", "fig"]
print(sorted(products, key=lambda p: p[1])) # by price
print(sorted(words, key=lambda w: w.lower())) # ignoring case
print(sorted(words, key=lambda w: (len(w), w.lower()))) # by length, then alphabetically[('Juice', 12000), ('Tea', 15000), ('Coffee', 45000)]
['Apple', 'banana', 'cherry', 'fig']
['fig', 'Apple', 'banana', 'cherry']Returning a tuple from the key sorts by several criteria: first by the first value, and ties by the second. To reverse one numeric criterion, negate it: key=lambda s: (-s["score"], s["name"]).
min() and max() accept the same key:
students = [
{"name": "Aziza", "score": 92},
{"name": "Bekzod", "score": 85},
]
best = max(students, key=lambda s: s["score"])
print(best["name"])Azizamap() and filter()
Two classic higher-order functions:
map(func, items)appliesfuncto every item.filter(func, items)keeps the items for whichfuncreturns a truthy value.
Both return lazy iterators, which hand out their items one at a time, only when asked. Printing one shows only <map object at 0x...>, so wrap them in list() to see the results:
numbers = [1, 2, 3, 4, 5, 6]
squares = list(map(lambda n: n * n, numbers))
evens = list(filter(lambda n: n % 2 == 0, numbers))
print(squares)
print(evens)[1, 4, 9, 16, 25, 36]
[2, 4, 6]The equivalent comprehensions are [n * n for n in numbers] and [n for n in numbers if n % 2 == 0]. In Python, comprehensions are usually preferred, because they read more naturally. map is neat when you already have a named function:
raw = ["12", " 7", "30 "]
print(list(map(int, raw))) # int() ignores surrounding spaces
print(list(map(str.strip, raw)))[12, 7, 30]
['12', '7', '30']Functions that return functions
A function can build and return a new function. The inner function remembers the values from the outer one (a closure, introduced in Variable Scope). This is a "function factory": configure once, use many times:
def make_price_formatter(currency, decimals=0):
def formatter(amount):
return f"{amount:,.{decimals}f} {currency}"
return formatter
som = make_price_formatter("som")
usd = make_price_formatter("USD", decimals=2)
print(som(1250000))
print(usd(99.5))1,250,000 som
99.50 USDAdvanced: lambdas created in a loop
functions = [lambda: i for i in range(3)]
print([f() for f in functions])
functions = [lambda i=i: i for i in range(3)]
print([f() for f in functions])[2, 2, 2]
[0, 1, 2]Each lambda in the first list looks up i when it's called, and by then the loop has finished with i = 2. Binding the current value as a default argument, lambda i=i: i, fixes it.
A glimpse of functools
The standard library's functools module has more tools for working with functions. Two you may meet (the from ... import ... line loads them, and Modules and Imports explains how imports work):
from functools import reduce, partial
numbers = [1, 2, 3, 4]
product = reduce(lambda acc, n: acc * n, numbers) # combine items into one value
print(product)
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2) # pre-fill an argument
print(square(9))24
81For most everyday tasks, a loop, a comprehension, or sum() is clearer than reduce().
When to use a lambda
| Use a lambda when… | Use def when… |
|---|---|
it's a short expression passed straight into sorted, max, map, … | it needs a name to be reused |
| it fits comfortably on one line | it needs several statements, loops, or error handling |
| its meaning is obvious from context | it deserves a docstring or tests |
Try it
Sort and filter the product list in different ways. Try sorting by name length, or keeping only products under 20,000.
Common mistakes
1. Calling the function instead of passing it
sorted(["b", "A"], key=str.lower())TypeError: unbound method str.lower() needs an argumentFix: key=str.lower, without parentheses.
2. Statements inside a lambda
f = lambda x: return x * 2SyntaxError: invalid syntaxA lambda body is a single expression, with no return, assignments, or loops. Fix: lambda x: x * 2, or use def.
3. Looping over a map object twice
result = map(str.upper, ["a", "b"])
print(list(result))
print(list(result))['A', 'B']
[]A map or filter object hands out its items only once; after that it's empty, with no error. Fix: convert it once, upper = list(map(...)), and reuse the list.
Practice
1. Sort by last letter
Sort words = ["python", "java", "rust", "go", "kotlin"] by their last letter.
Solution
words = ["python", "java", "rust", "go", "kotlin"]
print(sorted(words, key=lambda w: w[-1]))['java', 'python', 'kotlin', 'go', 'rust']"python" and "kotlin" both end with n, so they keep their original order (stable sort).
2. Longest and shortest
For cities = ["Tashkent", "Nukus", "Samarkand", "Jizzakh", "Urgench"], print the longest and shortest names using max and min with key.
Solution
cities = ["Tashkent", "Nukus", "Samarkand", "Jizzakh", "Urgench"]
print(max(cities, key=len))
print(min(cities, key=len))Samarkand
Nukus3. Leaderboard, the short way
Redo the leaderboard from Working with Lists and Sorting with a single sorted() call: sort by points (highest first), then by name alphabetically ignoring case.
players = [("ali", 120), ("Vali", 95), ("gani", 120), ("Sami", 150), ("Doni", 95)]Solution
players = [("ali", 120), ("Vali", 95), ("gani", 120), ("Sami", 150), ("Doni", 95)]
ranked = sorted(players, key=lambda p: (-p[1], p[0].lower()))
for rank, (name, points) in enumerate(ranked, start=1):
print(f"{rank}. {name:<5}{points:>4}")1. Sami 150
2. ali 120
3. gani 120
4. Doni 95
5. Vali 95Negating the points sorts them in descending order, while the names stay ascending.
4. Your own filter
Write keep_if(predicate, items) that returns a list of the items for which predicate(item) is True, without using filter(). Use it with a lambda to keep the words that start with a capital letter.
Solution
def keep_if(predicate, items):
return [item for item in items if predicate(item)]
words = ["Tashkent", "river", "Aziza", "book", "Python"]
print(keep_if(lambda w: w[0].isupper(), words))['Tashkent', 'Aziza', 'Python']5. Discount rules
Write make_discount(percent, minimum=0) that returns a function. The returned function takes a price and returns the discounted price, but only if the price is at least minimum. Otherwise it returns the price unchanged. Then apply several rules to a list of prices, and print the best (lowest) price for each.
rules = [make_discount(10), make_discount(20, minimum=100000), make_discount(30, minimum=500000)]
prices = [50000, 150000, 600000]Solution
def make_discount(percent, minimum=0):
def apply(price):
if price >= minimum:
return price * (1 - percent / 100)
return price
return apply
rules = [make_discount(10), make_discount(20, minimum=100000), make_discount(30, minimum=500000)]
prices = [50000, 150000, 600000]
for price in prices:
best = min(rule(price) for rule in rules)
print(f"{price:>8,} → {best:>10,.0f}") 50,000 → 45,000
150,000 → 120,000
600,000 → 420,000Adding a new discount rule means adding one line to rules, without touching the loop.
Recap
- Functions are values, and a higher-order function takes or returns them:
sorted,max,map, and your own. lambda params: expressioncreates a small anonymous function. Use it inline, and usedeffor anything bigger.key=lambda item: …controls sorting,min, andmax. Return a tuple to sort by several criteria.mapandfilterreturn lazy iterators, and comprehensions are usually more readable.- Functions can build functions that remember their configuration (closures).