Using Lambda with Built-in Functions
Lambda functions become particularly powerful when combined with Python's built-in functions like map()
, filter()
, and sorted()
. These combinations allow you to write efficient code for data transformation and manipulation.
The map()
Function with Lambda
The map()
function applies a given function to each item in an iterable (like a list) and returns a map object with the results.
-
Create a new file named lambda_builtin.py
in the /home/labex/project
directory.
-
Add the following code to demonstrate using map()
with lambda:
## Using map() with a lambda function to square each number in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print("Original numbers:", numbers)
print("Squared numbers:", squared_numbers)
- Run your code:
python3 ~/project/lambda_builtin.py
You should see:
Original numbers: [1, 2, 3, 4, 5]
Squared numbers: [1, 4, 9, 16, 25]
The filter()
Function with Lambda
The filter()
function creates a new iterable with elements that satisfy a condition (function returns True).
- Add the following code to your
lambda_builtin.py
file:
## Using filter() with a lambda function to find even numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("All numbers:", numbers)
print("Even numbers:", even_numbers)
## Using filter() to find names that start with 'J'
names = ["Alice", "Bob", "John", "Jane", "Michael", "Jessica"]
j_names = list(filter(lambda name: name.startswith('J'), names))
print("All names:", names)
print("Names starting with J:", j_names)
- Run your updated code:
python3 ~/project/lambda_builtin.py
You should see additional output:
All numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Even numbers: [2, 4, 6, 8, 10]
All names: ['Alice', 'Bob', 'John', 'Jane', 'Michael', 'Jessica']
Names starting with J: ['John', 'Jane', 'Jessica']
The sorted()
Function with Lambda
The sorted()
function allows you to sort iterables using a custom key function, which is where lambda functions are very useful.
- Add the following code to your
lambda_builtin.py
file:
## Using sorted() with lambda to sort by the second element of tuples
pairs = [(1, 5), (3, 2), (5, 7), (2, 9), (4, 1)]
sorted_by_second = sorted(pairs, key=lambda pair: pair[1])
print("Original pairs:", pairs)
print("Sorted by second element:", sorted_by_second)
## Using sorted() with lambda to sort strings by length
words = ["apple", "banana", "cherry", "date", "elderberry", "fig"]
sorted_by_length = sorted(words, key=lambda word: len(word))
print("Original words:", words)
print("Sorted by length:", sorted_by_length)
- Run your updated code:
python3 ~/project/lambda_builtin.py
You should see additional output:
Original pairs: [(1, 5), (3, 2), (5, 7), (2, 9), (4, 1)]
Sorted by second element: [(4, 1), (3, 2), (1, 5), (5, 7), (2, 9)]
Original words: ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']
Sorted by length: ['fig', 'date', 'apple', 'cherry', 'banana', 'elderberry']
Combining Multiple Lambda Functions
You can also chain or combine multiple operations using lambda functions:
- Add the following code to your
lambda_builtin.py
file:
## Combining map and filter with lambda functions
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
## First square the numbers, then filter for values greater than 20
result = list(filter(lambda x: x > 20, map(lambda x: x**2, numbers)))
print("Original numbers:", numbers)
print("Squared numbers > 20:", result)
- Run your updated code:
python3 ~/project/lambda_builtin.py
You should see additional output:
Original numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Squared numbers > 20: [25, 36, 49, 64, 81, 100]
Lambda functions with built-in functions like map()
, filter()
, and sorted()
provide a powerful way to process data with minimal code. In the next step, we'll explore more practical applications of lambda functions.