Sign in

What is a functor in Python?

A functor is a type that allows mapping elements to different values or types. By doing so, the container, the index of its elements and the total number of elements must not change.

An example is a Python list.

>>> numbers = [1, 2, 3]
>>> [x * 2 for x in numbers]
[2, 4, 6]

This is called functor mapping. Numbers is still a list, the length is the same and the order is the same. Only the values change.

So the list is the functor and the list comprehension is Python’s language feature for expressing the mapping.

In the following example, list is still a functor but the comprehension is not a functor mapping because the length of the list changes.

>>> numbers = [1, 2, 3]
>>> [x * 2 for x in numbers if x > 1]
[4, 6]

The word functor comes from a branch of mathematics called category theory and was created to emphasize the difference between function F that transforms A → B, and a functor that lets F operate on all its elements so all of them are transformed from A → B.

Written by Loek van den Ouweland on Jan. 21, 2026.