Decisions, Loops & FunctionsExtra· 35 min read

Lambda, map & filter

Tiny one-line functions, and tools that apply a function across a whole list.

What you will learn

  • Write a lambda (anonymous function)
  • Transform a list with map()
  • Filter a list with filter()

A function as a value

So far every function had a name (from def). But sometimes you need a tiny throwaway function just for one moment — and naming it would be overkill. A lambda is a one-line, anonymous (nameless) function. The word “anonymous” simply means it has no name of its own.

A key idea unlocks this lesson: in Python a function is just a value, like a number or a string. That means you can store it in a variable and even pass it to another function. A function that takes another function as an argument is called a higher-order function.

A lambda is a compact function
# a normal function...
def double(n):
    return n * 2

# ...and the exact same thing as a lambda
double2 = lambda n: n * 2

print(double(5))
print(double2(5))

The lambda reads lambda n: n * 2“take n, give back n * 2.” Everything after the colon is the value it returns; there is no return keyword and no name. We stored it in double2 here just to show it behaves identically to the def version — both print 10. In real code you usually pass a lambda straight into another function instead of naming it.

Note: Output: 10 10

map(): apply a function to every item

The built-in map() runs a function on every item of a list and gives back the transformed results. You hand it two things: the function to apply, and the list. (It returns a special map object, so we wrap it in list() to see the values.)

map() transforms each item
prices = [100, 250, 80]
with_tax = list(map(lambda p: p * 1.1, prices))
print(with_tax)

Here map takes the lambda lambda p: p * 1.1 and applies it to each price in turn: 100→110, 250→275, 80→88. The list(...) around it turns the result into a normal list we can print. So map is the tool way of saying “do this to every item.” (A list comprehension could do the same — both are common and correct.)

Note: Output: [110.00000000000001, 275.0, 88.0]

filter(): keep only matching items

The built-in filter() keeps only the items that pass a test. You give it a function that returns True or False, and the list. Items for which the function returns True are kept; the rest are dropped.

filter() keeps items that pass the test
numbers = [5, 12, 8, 3, 20]
big = list(filter(lambda n: n > 10, numbers))
print(big)

The lambda lambda n: n > 10 answers “is this number bigger than 10?” for each value. filter keeps the ones that answer True12 and 20 — and discards the rest. Again we wrap it in list() to view the result. So map transforms every item, while filter selects some items.

Note: Output: [12, 20]

When to use which

Here is a simple way to choose between the tools you now know for working over a list:

GoalReach for
Change every itemmap() or a comprehension
Keep only some itemsfilter() or a comprehension with if
A one-off tiny function to pass ina lambda
Anything complex / multi-linea named def function

Tip: If a lambda starts getting long or hard to read, that is a sign to write a proper named def function instead. Lambdas are best kept to a single, simple expression.

Q. What is a lambda in Python?

Answer: A lambda is a compact, anonymous function — lambda args: expression — handy for passing a quick function into map(), filter() and similar.

✍️ Practice

  1. Write a lambda that returns the square of a number and call it.
  2. Use filter() to keep only the names longer than 4 letters from a list.

🏠 Homework

  1. Given a list of prices, use map() to apply a 20% discount, then filter() to keep only those still above 100.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →