Python has many features for implementing functional programming concepts. When writing functional-style programs, you often need small functions that combine elements. Python has a built-in way to do this using lambda functions.
In computer programming, an anonymous function (such as a lambda expression) is a function not bound to an identifier. Lambda functions are an important part of functional programming that allow you to write throw-away functions without needing to name them.
In this Python tutorial, we will introduce you to lambda functions in Python and show you how to implement them in your own code.
Today, we will learn:
In this course, you’ll learn what functional programming is and how it’s used in Python. By the end, you’ll have a new programming paradigm under your belt.
Learn Functional Programming in Python
A lambda function is a small, anonymous function that take any number of arguments but only have one expression. Lambda functions return an object that is assigned to a variable or used as a part of other functions.
Lambdas differ from regular function definitions in several ways. Most notably, lambda functions are restricted to a single expression, so they can’t use statements or annotations.
When it comes to return values from lambdas, there is always an implicit return statement. Lambda functions evaluate the expression and automatically return a result.
This is why some programmers call lambdas “single expression functions”.
A lambda function does not need a name during function definition, unlike a normal function. We create them with the lambda keyword instead of the traditional def keyword. The structure of lambda can be seen below:
History: Lambda expressions comes from concepts in lambda calculus, a model of computation invented by Alonzo Church.
Though Python is not fully a functional language, it has added many functional concepts. In 1994, filter(), map(), reduce(), and the lambda operator were added to its syntax. Other object-oriented programming languages like Java and JavaScript also added lambda expressions in later versions.
Lambda functions have dozens of use cases, but they are most commonly used when function objects are required. The beauty of lambda functions is that they return function objects.
This makes them helpful when used alongside higher-order functions that require function objects as arguments, such as map(), filter(), or functools.reduce()
It is a best practice to use lambdas when the function expression is small to help with readability. It’s a good idea to use lambda functions when it provides the shortest way to write or compute something, for example, when:
reduce()Once you get used to lambda expressions, you’ll start using them quite often. They are expressive and make code shorter and more readable when used properly. To make the most of lambda, follow these general guidelines:
Learn Lambda expressions in Python without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments - making learning quick and efficient.
A lambda function is declared differently than a normal function. In Python, lambda functions have the following unique characteristics:
A lambda function in Python uses the following basic syntax. As we mentioned before, we use the lambda keyword to create a simple function in a Python expression.
lambda arguments: expression
A lambda expression can have any number of arguments (including none). For example:
lambda: 1 # No arguments
lambda x, y: x + y
lambda a, b, c, d: a*b + c*d
In the following example, we use a lambda function to replace an area function:
lambda x: x[0]*x[1]
The lambda keyword identifies the lambda expression. In the above example, x is the only parameter. The colon ends the parameter list and introduces the body of the function.
To use this expression properly, you place it wherever you might normally use a function object. This piece of code below creates a temporary, anonymous function object and passes it into the sorted function, which then performs the sort.
Note: Lambda functions do not have names, but if you really want to, you can assign it to a variable, like below:
area = lambda x: x[0]*x[1]There is no overt benefit to adding a function name to a lambda function.
Let’s apply what we’ve learned so far to two hands-on examples. First, write a chunk of code using a lambda expression that adds 10 to argument a and returns the result. You can select any value for a. Try it yourself before checking the solution.
For another activity, use lambda to multiply a with argument b and return the result. You can choose ant numbers to multiply when you print. Try it yourself before checking the solution.
Before you start using lambdas everywhere, it’s important to understand what they can’t do. Lambda functions in Python are meant for simple, single-expression tasks, and they come with a few built-in limitations:
You can’t include statements like if, for, while, try/except, or return.
You can’t assign variables or import modules inside a lambda.
You can’t use await inside a lambda (so async operations require a regular function).
Because of these restrictions, lambdas are best used for short, inline logic rather than anything complex. If your function needs multiple steps, use a regular def instead—your future self (and your teammates) will thank you for it.
While lambdas are handy, overusing them can make your code harder to read, debug, and maintain. A good rule of thumb is:
Use a lambda for short, throwaway functions — like sorting, filtering, or simple calculations.
Avoid lambdas for complex logic, error handling, or anything that spans multiple lines.
For example:
# Hard to read and debugresult = list(map(lambda x: x**2 if x > 0 else -x, numbers))# Much clearerdef transform(x):return x**2 if x > 0 else -xresult = list(map(transform, numbers))
In real-world projects, readability often matters more than brevity. If a lambda makes your code confusing, switch to a named function.
Because lambdas are anonymous and don’t have a name, debugging them can sometimes be tricky. Here are a few best practices:
Avoid chaining multiple lambdas in a single line. This makes tracebacks harder to follow.
Give lambdas descriptive variable names when assigning them to variables.
Use named functions when debugging is likely — stack traces will show meaningful names instead of <lambda>.
For example:
# Hard to debugprocess = map(lambda x: x * 2, items)# Easier to debugdouble = lambda x: x * 2process = map(double, items)
Here are a few style guidelines most Python developers follow when writing lambdas:
Keep them short and readable — one simple expression is ideal.
Prefer named functions for anything you might reuse later.
Use lambdas where they make the code more declarative, like sorted, map, filter, or key functions.
Don’t sacrifice clarity for conciseness.
When in doubt, ask yourself: Would a future teammate (or future me) understand this quickly? If the answer is “no,” use a regular function.
Congrats! You should now have a solid understanding of lambda expressions in Python. You can can start implementing them in your own code. There is still more to learn when it comes to Python functional programming! Next you should check out:
To get started with functional programming in Python, check out Educative’s course Learn Functional Programming in Python. In this course, you’ll learn what functional programming is, how it’s used in Python. You will everything from lambda to perform recursion to generators, and more.
By the end, you’ll have the confidence to use functional programming in your projects.
Happy learning!