Lambda Functions in Python Explained

Lambda functions are a short way to create small functions in Python.

This page explains:

  • what a lambda function is
  • what its syntax means
  • where beginners usually see it
  • when def is a better choice

If you are new to functions, it also helps to understand what a function is in Python and how Python functions work.

Quick example

add = lambda a, b: a + b
print(add(2, 3))

numbers = [1, 2, 3, 4]
doubled = list(map(lambda n: n * 2, numbers))
print(doubled)

Output:

5
[2, 4, 6, 8]

Use lambda for short, one-expression functions. If the logic needs multiple steps, use def instead.

What a lambda function is

A lambda function is a small anonymous function.

Here is what that means:

  • Small means it is usually used for short logic.
  • Anonymous means it does not need a normal function name.
  • It is created with the lambda keyword.
  • It can take arguments.
  • It returns the result of one expression.

Example:

square = lambda x: x * x
print(square(4))

Output:

16

Even though lambda functions are called "anonymous", you can still assign one to a variable like square. The main difference is that the function itself is written with lambda instead of def.

Basic lambda syntax

The general form is:

lambda parameters: expression

Parts of the syntax

  • The parameters go before the colon.
  • The expression goes after the colon.
  • The result of the expression is returned automatically.
  • You do not write return inside a lambda.

Example with one argument:

double = lambda x: x * 2
print(double(5))

Output:

10

Example with two arguments:

add = lambda a, b: a + b
print(add(2, 3))

Output:

5

This works like a normal function written with def:

def add(a, b):
    return a + b

print(add(2, 3))

If you want to understand arguments more clearly, see function parameters and arguments in Python and return values in Python functions.

Lambda vs normal functions

Use lambda for very short and simple functions.

Use def when:

  • the function needs multiple lines
  • the logic has more than one step
  • you want clearer code
  • you plan to reuse the function in several places
  • debugging would be easier with a named function

Lambda example

multiply = lambda a, b: a * b
print(multiply(3, 4))

The same logic with def

def multiply(a, b):
    return a * b

print(multiply(3, 4))

Both versions accept arguments and return values.

For beginners, def is often easier to read. Lambda is most useful when you need a small function only once.

Where beginners usually see lambda

Beginners often see lambda functions as short helper functions passed into other functions.

With map() to change each item

map() applies a function to every item in an iterable.

numbers = [1, 2, 3]
doubled = list(map(lambda n: n * 2, numbers))
print(doubled)

Output:

[2, 4, 6]

Here, lambda n: n * 2 takes each number and doubles it.

For more details, see Python map() explained.

With filter() to keep matching items

filter() keeps only items where the function returns True.

numbers = [1, 2, 3, 4]
result = list(filter(lambda n: n > 2, numbers))
print(result)

Output:

[3, 4]

Here, only numbers greater than 2 stay in the result.

See also Python filter() explained.

With sorted() as a key function

A lambda is often used to tell sorted() what value to sort by.

pairs = [('a', 3), ('b', 1), ('c', 2)]
result = sorted(pairs, key=lambda item: item[1])
print(result)

Output:

[('b', 1), ('c', 2), ('a', 3)]

This sorts by the second item in each tuple.

See Python sorted() explained.

Inside short one-time helper expressions

You can also call a lambda immediately:

print((lambda x: x * 2)(5))

Output:

10

This is valid Python, but it is less common in beginner code.

Important limits of lambda

Lambda functions have important limits.

  • A lambda can contain only one expression.
  • It cannot contain normal statements in the usual form, such as:
    • assignment statements
    • regular if blocks
    • for loops
  • Complex lambda code becomes hard to read very quickly.
  • Readable code is usually better than shorter code.

This is not valid:

# Invalid Python
# func = lambda x:
#     y = x + 1
#     return y

A lambda must stay on one expression.

If you need multiple steps, use def:

def func(x):
    y = x + 1
    return y

print(func(5))

Output:

6

When not to use lambda

Do not use lambda when:

  • the logic takes more than one step
  • the function will be reused in many places
  • a named function makes the code easier to understand
  • debugging would be easier with def

Example where def is better:

def format_name(name):
    cleaned = name.strip()
    return cleaned.title()

print(format_name("  alice "))

Output:

Alice

This would be harder to read as a lambda because it does more than a very small one-time task.

Also, sometimes a list comprehension is clearer than map() with lambda.

For example, these do the same thing:

numbers = [1, 2, 3]
doubled = list(map(lambda n: n * 2, numbers))
print(doubled)
numbers = [1, 2, 3]
doubled = [n * 2 for n in numbers]
print(doubled)

Many beginners find the list comprehension easier to read.

Common mistakes

Here are some common beginner mistakes with lambda functions.

Trying to write multiple statements inside a lambda

This does not work because lambda allows only one expression.

Use def if you need multiple steps.

Using lambda when a normal function would be clearer

Shorter code is not always better.

If a named function helps explain the code, use def.

Forgetting that lambda returns one expression automatically

You do not write return inside a lambda.

This is correct:

add = lambda a, b: a + b
print(add(2, 3))

Confusing lambda syntax with function calls

This creates a function:

double = lambda x: x * 2

This calls the function:

print(double(5))

Using map() or filter() without converting to a list when needed

In Python 3, map() and filter() return iterator objects.

If you want to see all the results at once, wrap them with list():

print(list(map(lambda n: n * 2, [1, 2, 3])))
print(list(filter(lambda n: n > 2, [1, 2, 3, 4])))

Helpful test examples:

print(add(2, 3))
print((lambda x: x * 2)(5))
print(list(map(lambda n: n * 2, [1, 2, 3])))
print(list(filter(lambda n: n > 2, [1, 2, 3, 4])))
print(sorted([('a', 3), ('b', 1)], key=lambda item: item[1]))

Expected output:

5
10
[2, 4, 6]
[3, 4]
[('b', 1), ('a', 3)]

FAQ

What is a lambda function in Python?

It is a short function written in one line with the lambda keyword.

Why is it called anonymous?

Because it does not need a regular function name like one created with def.

Can lambda have multiple lines?

No. A lambda is limited to a single expression.

Is lambda better than def?

Not always. Lambda is useful for short one-time functions, but def is usually clearer for larger logic.

Does lambda return a value?

Yes. It automatically returns the result of its expression.

See also