14
Lesson 14
Using lambda functions with map(), filter(), and sorted()
Lambda functions are often used in combination with built-in Python functions like map(), filter(), and sorted() to create concise and efficient code. These functions are powerful tools for manipulating collections such as lists. Below, we’ll explore how to use lambda functions with each of these functions, along with examples.
1. Using map() with Lambda functions:
The map() function applies a given function to all items in an iterable (like a list) and returns a map object (which is an iterator). When used with lambda functions, it allows you to apply simple transformations to each element in the iterable.
1. Using map() with Lambda functions:
The map() function applies a given function to all items in an iterable (like a list) and returns a map object (which is an iterator). When used with lambda functions, it allows you to apply simple transformations to each element in the iterable.
# List of numbers numbers = [1, 2, 3, 4, 5] # Using map with a lambda function to square each number squared = list(map(lambda x: x ** 2, numbers)) print(squared) # Output: [1, 4, 9, 16, 25]
In this example:
- The lambda function lambda x: x ** 2 takes an input x and returns its square.
- map() applies this lambda function to each element in the numbers list, resulting in a new list of squared values.
2. Using filter() with Lambda functions:
The filter() function filters the elements of an iterable based on a function that returns True or False. It constructs a new iterable containing only the elements for which the function returns True. Lambda functions can be used to define this filtering condition concisely.
# List of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Using filter with a lambda function to get even numbers even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) # Output: [2, 4, 6, 8]
In this example:
- The lambda function lambda x: x % 2 == 0 checks if a number is even.
- filter() applies this lambda function to each element in the numbers list and returns a new list containing only the even numbers.
3. Using sorted() with Lambda functions:
The sorted() function sorts the elements of an iterable based on a specified key function. When using a lambda function, you can customize the sorting behavior easily.
# List of tuples representing (name, age) people = [("Alice", 30), ("Bob", 25), ("Charlie", 35)] # Using sorted with a lambda function to sort by age sorted_people = sorted(people, key=lambda person: person[1]) print(sorted_people) # Output: [('Bob', 25), ('Alice', 30), ('Charlie', 35)]
In this example:
- The lambda function lambda person: person[1] extracts the age from each tuple.
- sorted() uses this lambda function as the key to sort the people list based on the age of each person.
Summary
- map(): Use lambda functions to apply a transformation to each item in an iterable, returning a new iterable with the transformed values.
- filter(): Use lambda functions to define conditions for filtering items in an iterable, returning a new iterable with only those items that meet the condition.
- sorted(): Use lambda functions to specify custom sorting criteria, allowing you to sort items based on specific attributes or computed values.