Python Lambda Functions: A Practical Guide
Introduction to Python Lambda Functions
Lambda functions in Python are small, anonymous functions defined with the 'lambda' keyword. They are useful for creating simple functions without needing to formally define a function using 'def'. This tutorial will guide you through various examples of using lambda functions, focusing on practical usage.
Consider the following function:
Code:
The same function can be defined using lambda expression as follows:
Code:
Both functions are the same. Note that lambda does not include a return statement. The right expression is the implicit return value. Lambda functions need not to be assigned to any variable.
Example 1: Basic Lambda Function
This example demonstrates a simple lambda function that adds 10 to a given number. Lambda functions are ideal for small, single-use functions like this.
Code:
Explanation:
- Lambda Function: 'lambda x: x + 100' creates an anonymous function that takes one argument 'x' and returns 'x + 100'.
- Usage: We assign the lambda function to the variable ‘add_ten’, then use it to add 100 to the number 25.
Example 2: Lambda Function with Multiple Arguments
This example shows a lambda function with multiple arguments, which multiplies two numbers. It highlights how lambda functions can handle simple operations with more than one input.
Code:
Explanation:
- Lambda Function: 'lambda x, y: x * y' defines a function that takes two arguments, 'x' and 'y', and returns their product.
- Usage: We use the lambda function to multiply 5 and 3, resulting in 15.
Example 3: Lambda Function with 'map()'
This example demonstrates using a lambda function with 'map()' to apply an operation to each element in a list, specifically squaring each number.
Code:
Explanation:
- Lambda Function: 'lambda x: x ** 2' defines a function that squares its input.
- 'map()' Function: Applies the lambda function to each element in the 'numbers' list.
- Usage: The 'map()' function returns a map object, which we convert to a list of squared numbers.
Example 4: Lambda Function with 'filter()'
This example shows how to use a lambda function with filter() to select elements from a list that meet a certain condition—in this case, filtering out even numbers.
Code:
Explanation:
- Lambda Function: 'lambda x: x % 2 == 0' defines a function that returns 'True' if 'x' is even.
- 'filter()' Function: Filters the numbers list, returning only the even numbers.
- Usage: The 'filter()' function returns a filter object, which we convert to a list of even numbers.
Example 5: Lambda Function with 'reduce()'
This example demonstrates using a lambda function with ‘reduce()’ to perform a cumulative operation on a list of elements, specifically calculating the product of all numbers.
Code:
Explanation:
- Lambda Function: 'lambda x, y: x * y' multiplies two numbers.
- 'reduce()' Function: Applies the lambda function cumulatively to the items in the 'numbers' list, reducing them to a single value.
- Usage: The 'reduce()' function returns the product of all numbers in the list.
Example 6: Lambda Function in Sorting
This example illustrates using a lambda function with sorted() to sort a list of tuples by a specific element—in this case, sorting people by age.
Code:
Explanation:
- Lambda Function: 'lambda person: person[1]' extracts the second element (age) from each tuple.
- 'sorted()' Function: Sorts the 'people' list based on the age of each person.
- Usage: The 'sorted()' function returns a new list of tuples sorted by age.
Example 7: Lambda Function in Dictionary Operations
This example demonstrates using a lambda function with ‘max()’ to find the key associated with the highest value in a dictionary, such as identifying the most expensive item.
Code:
Explanation:
- Lambda Function: 'lambda item: prices[item]' returns the price of the item.
- 'max()' Function: Finds the item with the highest price.
- Usage: The 'max()' function returns the key (item) associated with the maximum value (price) in the dictionary.