Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 149 ยท Functions

Higher-Order Functions

Treat functions as values in Python - pass them as arguments, return them from other functions, and use map, filter, sorted, min, max and reduce effectively.

In Python, functions are first-class objects. You can store them in variables, put them in lists and dictionaries, pass them to other functions and return them as results, exactly like numbers or strings. A function that takes another function as an argument, or returns one, is called a higher-order function.

You've already used a few: sorted(..., key=...) in Lambda Functions, and make_multiplier in Return Values. This lesson pulls those ideas together. You'll write your own higher-order functions, learn the built-in ones (map, filter, sorted, min, max, any, all) and meet functools.reduce and partial.

Functions are values

A function's name is just a variable that refers to a function object. Without parentheses, you're talking about the function; with parentheses, you're calling it.

Python Functions as values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice str.strip in the dictionary: methods can be passed around too. str.strip(" hi ") is the same as " hi ".strip().

Passing functions as arguments

Once functions are values, you can write general-purpose code that leaves one step open and lets the caller fill it in. Here, apply_to_all knows how to walk a list but not what to do with each item:

Python Your own higher-order function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Callable[[float], float] is a type hint meaning "a function that takes one float and returns a float". It's optional, but it documents what kind of function the caller should pass. The retry example shows a common real-world pattern: the caller supplies the what, and the higher-order function supplies the how (here, retrying).

map and filter

Two built-ins apply a function across an iterable:

  • map(func, iterable) calls func on every item and yields the results.
  • filter(func, iterable) yields only the items for which func returns a truthy value.

Both return lazy iterators, not lists: they compute values only when you ask for them. Wrap them in list() to see everything at once.

Python map and filter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note: A map or filter object can be consumed only once. After list(lengths) used it up, a second list(lengths) would be empty. You'll see why in Generators and Iterators.

Comprehensions or map?

In Python, a list comprehension often reads better than map and filter, especially when you would need a lambda:

Python
[w.upper() for w in words if len(w) > 4]                  # clear
list(map(str.upper, filter(lambda w: len(w) > 4, words)))  # harder to read

map is a good fit when you already have a named function, as in map(int, "1 2 3".split()). Otherwise, prefer comprehensions.

sorted, min and max with key

The key parameter turns sorted(), list.sort(), min() and max() into higher-order functions. You pass a function that extracts the value to compare, and Python calls it on each item.

Python Choosing by key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without a key, uppercase letters sort before lowercase ones because they come first in Unicode. key=str.casefold compares the case-folded versions while still returning the original strings.

Passing, not calling

The most common mistake with higher-order functions is calling the function instead of passing it. sorted(words, key=len) hands sorted the function len, which it calls on each word. sorted(words, key=len()) calls len immediately with no arguments, which raises a TypeError before sorted even starts. Likewise, map(str.upper, words) is right, while map(str.upper(), words) is not. When a parameter expects a function, give it the name without parentheses, or a lambda.

any and all

any() and all() aren't higher-order themselves, but they pair perfectly with generator expressions to ask a question about every item:

Python
passwords = ["hunter2", "correct-horse", "abc"]
print(all(len(p) >= 6 for p in passwords))  # False: "abc" is too short
print(any(len(p) >= 12 for p in passwords)) # True

Both stop as soon as the answer is known, so they don't check more items than necessary.

Returning functions

A higher-order function can also build a function and return it. The returned function is a closure that remembers the arguments it was built with (see Scope). This is handy for creating families of related functions:

Python Function factories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

compose is both kinds of higher-order function at once: it takes functions as arguments and returns a new one. Pipelines like this keep each step small and testable.

functools.partial

Often you want a version of an existing function with some arguments already filled in. You could write a small def or lambda, but functools.partial does it directly:

Python partial

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A partial object behaves like a function and remembers the frozen arguments. It's especially useful for callbacks and map, where the function must take exactly one argument.

functools.reduce

reduce folds an iterable into a single value by applying a two-argument function repeatedly: first to items 1 and 2, then to that result and item 3, and so on.

Python
from functools import reduce

numbers = [3, 8, 2, 5]
largest = reduce(lambda a, b: a if a > b else b, numbers)   # 8
product = reduce(lambda a, b: a * b, numbers, 1)             # 240

The third argument is the starting value, which is also returned if the iterable is empty. That said, Python has dedicated built-ins for the most common reductions (sum, max, min, any, all, and math.prod), and they're clearer. Reach for reduce only when none of those fits. The itertools and functools lesson covers more tools in this family.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What makes a function "higher-order"?

    Higher-order functions work with other functions as values, like sorted with key= or a function factory.
  2. 2. What does map(len, ["hi", "hey"]) return?

    map returns an iterator. Wrap it in list() to get [2, 3].
  3. 3. What does list(filter(None, [0, 1, "", "a", None])) produce?

    With None as the function, filter keeps only truthy items.
  4. 4. Given f = partial(round, ndigits=1), what is f(3.14159)?

    The partial object calls round(3.14159, ndigits=1), which gives 3.1.
  5. 5. Which is generally the most Pythonic way to get the squares of the even numbers in nums?

    A list comprehension expresses the transform and the condition in one readable line.

Summary

  • Functions are first-class objects: you can assign, store, pass and return them. Writing a name without () refers to the function itself.
  • A higher-order function takes a function as an argument or returns one.
  • map() and filter() return lazy iterators; comprehensions are often clearer, especially when you'd need a lambda.
  • sorted(), min() and max() accept a key function; methods like str.casefold work as keys too.
  • Function factories return closures; functools.partial freezes some arguments of an existing function.
  • functools.reduce folds an iterable to one value, but prefer sum, max, min, any, all and math.prod when they fit.

Next: Decorators, which use higher-order functions to add behavior to existing functions.