In this article we will explore how to use Python lambda functions.
Table of Contents
- Introduction
- Lambda function with one argument
- Lambda function with multiple arguments
- Use lambda function with other Python functions
- Conclusion
Introduction
In Python, a lambda function is a function that can take any number of arguments but can only have one expression.
The lambda function syntax is defined as:
lambda arguments: expression
Lambda function with one argument
A simple example of Python lambda function is lambda function with one argument.
For example, let’s create a lambda function that will take some number x and add 5 to it:
#Create a lambda function to add 5 to a given number
add_five = lambda x: x+5
#Test lambda function
result = add_five(3)
print(result)
and you should get:
8
Lambda function with multiple arguments
Python lambda functions can get more complex as they grow in a number of arguments, but they are capable of handling more complex tasks.
For example, let’s create a lambda function that will take two numbers x and y and multiply them:
#Create a lambda function to multiply numbers
multiply = lambda x, y: x*y
#Test lambda function
result = multiply(3, 5)
print(result)
and you should get:
15
Use lambda function with other Python functions
In most cases, when you see Python lambda functions in the code, they are often used with other Python functions such as map(), sorted(), and others.
Use lambda function with map()
One of the most popular uses of lambda functions is with Python map() function.
For example, let’s consider the same one argument lambda function from previous section, and extend its functionality to work with a list.
We will create a list of numbers nums and use the lambda function to add 5 to each of the numbers in the nums list.
#Create a list with numbers
nums = [1, 2, 3]
#Map a lambda function to add 5 to each number in a list
mapped = map(lambda x: x+5, nums)
#Convert map object to a list
result = list(mapped)
#Print result
print(result)
and you should get:
[6, 7, 8]
Use lambda function with sorted()
Another popular application of lambda functions is with Python sorted() function, where lambda function is used as a ‘key’ argument for sorting.
For example, let’s create a list of tuples with fruit names and prices, and sort this list based on the price of the fruit in ascending order.
#Create a list of tuples with fruits and prices
fruit_prices = [('Banana', 3), ('Apple', 1), ('Orange', 5)]
#Sort the list based on the price of the fruit
sorted_prices = sorted(fruit_prices, key=lambda x: x[1])
#Print result
print(sorted_prices)
and you should get:
[('Apple', 1), ('Banana', 3), ('Orange', 5)]
The list of tuples is now sorted based on the price.
Conclusion
In this article we explored how to use Python lambda functions.
Now that you know the basic functionality, you can practice using it with other iterable data structures for more complex use cases.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.