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.
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,
reduce(), and the
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
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:
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*x
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.
p = [(3, 3), (4, 2), (2, 2), (5, 2), (1, 7)] q = sorted(p, key=lambda x: x*x) print(q) # [(2, 2), (1, 7), (4, 2), (3, 3), (5, 2)]
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*x
There is no overt benefit to adding a function name to a lambda function.
# A squaring lambda function square = lambda n : n*n num = square(5) print num
# A subtraction lambda function with multiple arguments sub = lambda x, y : x-y print(sub(5, 3))
A Python lambda makes the
map function far more concise.
Lambdas can also simplify the
x = # type your code here print # type your code here
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.
x = # write your code here print # write your code here
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.
Join a community of 500,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.