Quick Answer

A lambda is a small unnamed function written inline. map applies a function to every item, filter keeps items matching a condition. In Python, a list comprehension usually reads better than map or filter — but lambda as a sort key is genuinely idiomatic.

lambda is just a function without a name

def square(n):
    return n * n

square = lambda n: n * n     # the same thing, written inline

The restriction is that a lambda contains a single expression — no statements, no loops, no multiple lines. That is deliberate: if it needs more than an expression, it should be a real function with a name that says what it does.

Assigning a lambda to a variable, as above, is pointless — you have created a named function the awkward way. Lambdas earn their place when passed directly to something else, which is what the rest of this article is about.

map and filter

nums = [1, 2, 3, 4, 5, 6]

print(list(map(lambda n: n*n, nums)))
# [1, 4, 9, 16, 25, 36]

print(list(filter(lambda n: n % 2 == 0, nums)))
# [2, 4, 6]

map applies a function to every item; filter keeps the items for which the function returns True. Both return lazy iterators in Python 3, which is why list() is needed to see the result — printing them directly shows something like <map object at 0x...>, which confuses a lot of beginners.

The laziness is useful in a pipeline over a large file, because nothing is computed until consumed. For a short list you will convert immediately anyway.

The comprehension usually wins

print([n*n for n in nums])            # [1, 4, 9, 16, 25, 36]
print([n for n in nums if n % 2 == 0])  # [2, 4, 6]

Identical results, and most Python developers find these easier to read. There is no lambda keyword, no wrapping in list(), and the condition sits where you expect it.

This is a genuine difference between Python and languages where map and filter are the idiom. Python has comprehensions, and its community broadly prefers them. If you write map(lambda ...) in an interview, expect to be asked whether a comprehension would be clearer — and the honest answer is usually yes.

map stays clean when the function already exists and needs no lambda: list(map(str.upper, names)) reads perfectly well.

Where lambda is genuinely the right tool

Sorting by a computed value. This is the most common real use, and there is no neater alternative:

people = [("Asha", 91), ("Ravi", 68), ("Meera", 96)]

print(sorted(people, key=lambda p: p[1], reverse=True))
# [('Meera', 96), ('Asha', 91), ('Ravi', 68)]

The key function tells sorted what to compare. Sorting a list of dictionaries by a field, strings by length, or records by date all follow this shape, and it appears constantly in real code.

The same applies to max and min: max(people, key=lambda p: p[1]) gives the highest scorer rather than the alphabetically last name.

reduce, and why it was moved out

from functools import reduce

print(reduce(lambda a, b: a + b, nums))   # 21

reduce collapses a sequence to a single value by repeatedly combining pairs. It was moved out of the builtins in Python 3 deliberately — for the common cases, clearer functions already exist. Summing is sum(nums). Finding a maximum is max(nums). Joining strings is "".join(parts).

reduce is worth reaching for only when the combining operation is genuinely custom and has no built-in equivalent. If you find yourself writing reduce(lambda a, b: a + b, ...), use sum.

Being able to explain why it was demoted is a good interview answer, because it shows you understand that Python favours readable specific tools over general abstract ones.

Frequently Asked Questions

Should I use map and filter or list comprehensions? Comprehensions in most cases — they are the more idiomatic Python and generally read better. map is fine when you are passing an existing named function rather than writing a lambda.
Why does printing map() show a map object? Because map returns a lazy iterator rather than a list in Python 3. Wrap it in list() to materialise the values. The laziness is useful for large inputs where you consume items one at a time.
Can a lambda have multiple statements? No. A lambda contains one expression only — no assignments, loops or multiple lines. If you need those, define a normal function, which is also better because it can have a descriptive name.
What is the key argument in sorted? A function applied to each element to decide what to compare. sorted(people, key=lambda p: p[1]) sorts by the second element of each tuple rather than the first. It is the most common practical use of lambda.
Is functional programming worth learning in Python? The ideas are worth knowing — pure functions and avoiding shared mutable state make code easier to reason about. Python supports them but is not a functional language, so apply the ideas without forcing the syntax.