What Is a Lambda Function in Python?

A lambda function in Python is a small function written in one line.

Beginners usually see lambda functions in examples with sorting, map(), or filter(). They are useful for short tasks, but they are not a replacement for normal functions.

A lambda function works like a regular function. The main difference is that it is written in a shorter form.

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

Output:

25

A lambda function is a small anonymous function written in one line.

Simple definition

A lambda function:

  • is a short function written in one line
  • can take inputs and return a result
  • is often called an anonymous function because it does not need a normal function name
  • uses the lambda keyword

Here is a simple example:

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

Output:

8

Even though lambda functions are often called anonymous, you can still store one in a variable, as shown above.

If you are new to functions, see what a function is in Python.

Basic syntax

The basic pattern is:

lambda parameters: expression

How to read this:

  • the part before the colon is the input
  • the part after the colon is the value that gets returned
  • there is no return keyword inside a lambda

Example:

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

Output:

10

In this example:

  • a and b are parameters
  • a + b is the expression that Python returns

If you want to understand the difference between inputs in a function definition and values passed into a function call, see what a parameter is in Python and what an argument is in Python.

How it compares to def

A regular function uses def and can contain multiple lines. A lambda function is limited to a single expression.

These two examples do the same thing:

def square(x):
    return x * x

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

Output for both:

25

Use def when:

  • the logic is longer
  • you need multiple steps
  • you want a clear function name
  • readability matters more than saving space

Use lambda when:

  • the operation is short
  • you only need the function once
  • the code stays easy to read

For a broader explanation, see Python functions explained and lambda functions in Python explained.

Where beginners may see lambda

Beginners often see lambda used when a function is passed into another function.

With sorted()

A common example is sorting data by a specific part of each item.

students = [("Maya", 82), ("Leo", 91), ("Ava", 78)]

result = sorted(students, key=lambda student: student[1])
print(result)

Output:

[('Ava', 78), ('Maya', 82), ('Leo', 91)]

Here, lambda student: student[1] tells sorted() to use the second value in each tuple for sorting.

See Python sorted() function explained if you want to learn more about the key argument.

With map()

numbers = [1, 2, 3, 4]

result = list(map(lambda x: x * 2, numbers))
print(result)

Output:

[2, 4, 6, 8]

This applies the lambda function to each item in the list.

See Python map() function explained.

With filter()

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

result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)

Output:

[2, 4, 6]

This keeps only the values where the lambda returns True.

See Python filter() function explained.

When to use it

Use a lambda function when:

  • you need a small one-time function
  • the code is still easy to read
  • writing a full def function would feel unnecessary

Avoid lambda when:

  • the expression becomes hard to understand
  • you need multiple statements
  • the function will be reused often
  • a named function would make the code clearer

A good rule for beginners is simple: if the lambda makes the code harder to read, use def instead.

Common beginner confusion

Some common points are easy to misunderstand:

  • Lambda is still a function. It is not a special separate kind of thing.
  • It can be stored in a variable. You can assign it just like other values.
  • It does not run automatically. Creating a lambda only creates the function. You still need to call it.
  • It is not always better than def. Shorter code is not always clearer code.

Example:

my_lambda = lambda x: x + 1

print(type(my_lambda))
print(my_lambda(3))

Possible output:

<class 'function'>
4

This shows that a lambda is a normal function object.

Common mistakes

Beginners often run into these problems:

  • Thinking lambda is a different kind of object instead of a function
  • Trying to write multiple statements inside a lambda
  • Using lambda where a normal named function would be easier to read
  • Confusing parameters with arguments in lambda examples

For quick checking, these commands can help:

print(type(my_lambda))
print(my_lambda(3))
help(sorted)
help(map)
help(filter)

These can help you confirm:

  • whether your lambda is really a function
  • what result it returns
  • how functions like sorted(), map(), and filter() expect lambda to be used

FAQ

Does a lambda function return a value?

Yes. The expression after the colon is returned automatically.

Can a lambda function have more than one parameter?

Yes. You can write multiple parameters separated by commas.

Example:

multiply = lambda a, b: a * b
print(multiply(2, 5))

Can a lambda function contain multiple lines?

No. A lambda is limited to one expression.

Should I always use lambda instead of def?

No. Use def when the function is longer, reused, or needs to be easier to read.

See also