python tutorial
Python Lambda
Learn more about Python, one of the world’s most versatile and popular programming languages.
Functions in Python are defined using the def
keyword and always have a name that describes what the function does. However, Python also allows developers to write functions that do not have a name. These functions are called anonymous functions and they are defined using the lambda
keyword. Python Lambda Functions are also called Lambda Expressions.
Any lambda function or expression is of the following form:
lambda argument(s): function logic or expression
Python Functions vs Lambda Expressions
Let’s take a look at how Python Lambda functions or expressions differ from normal Python functions. We will take an example of a function that multiplies a number by 10:
>>> def multiply_by_ten(num):
… return num * 10;
…
>>> print(multiply_by_ten(10)) # prints 100
100
>>> print(multiply_by_ten(20)) # prints 200
200
>>> print(multiply_by_ten(50)) # prints 500
500
# same but with a lambda function
>>> multiply_by_ten_lambda = lambda num: num * 10
>>> print(multiply_by_ten_lambda (10)) # prints 100
100
>>> print(multiply_by_ten_lambda(20)) # prints 200
200
>>> print(multiply_by_ten_lambda(50)) # prints 500
500
As seen above, Python lambda functions or expressions are very useful when there is a need for an expression-like temporary function for which giving a name is not necessary. Python lambda functions or expressions are also useful when you need a function as an argument to other functions in Python. For example, functions like filter()
and map()
are very popular in languages like Python. filter()
takes an anonymous function or lambda expression and a list as parameters and filters out all the values from the list that do not match a condition or predicate. map()
on the other hand takes an anonymous function or lambda expression and a list as parameters and produces an altogether new list of values based on certain logic.
Let’s take a look at these examples:
# make the dog breed plural by appending `s` to it
>>> dog_breeds = ['Poodle', 'Bulldog', 'Boxer', 'Chihuahua']
>>> print(dog_breeds)
['Poodle', 'Bulldog', 'Boxer', 'Chihuahua']
>>> dog_breeds_plural = list(map(lambda breed: breed + 's', dog_breeds))
>>> print(dog_breeds_plural)
['Poodles', 'Bulldogs', 'Boxers', 'Chihuahuas']
# filter all the failing grades less than or equal to 35
>>> grades = [45, 89, 34, 67, 90, 55, 98, 35, 12, 84]
>>> print(grades)
[45, 89, 34, 67, 90, 55, 98, 35, 12, 84]
>>> passing_grades = list(filter(lambda grade: grade > 35, grades))
>>> print(passing_grades)
[45, 89, 67, 90, 55, 98, 84]
As seen in above examples, we use lambda expressions to:
- Make all the dog breed names in a list as plural value by using
map()
- Filter out all the grades that are less than or equal to 35 by using
filter()
Learn Python Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required