Using Lists with Functions (e.g., map, filter, reduce) in Python Code
In Python, the map, filter, and reduce functions are powerful tools for working with lists (or any iterable). These functions allow you to apply a function to each item in a list, filter items based on a condition, or reduce a list to a single value, respectively. Here's a detailed look at each function:
map
The map function applies a given function to all items in an input list (or any iterable). The syntax is:
Python Code
map(function, iterable)
Example:
Python Code
# Example function to square a number
def square(x):
return x * x
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Applying map function
squared_numbers = list(map(square, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Using a lambda function:
Python Code
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
filter
The filter function creates a list of elements for which a function returns True. The syntax is:
Python Code
filter(function, iterable)
Example:
Python Code
# Example function to check if a number is even
def is_even(x):
return x % 2 == 0
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Applying filter function
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # Output: [2, 4]
Using a lambda function:
Python Code
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Output: [2, 4]
reduce
The reduce function applies a rolling computation to sequential pairs of values in a list, resulting in a single value. This function is part of the functools module. The syntax is:
Python Code
from functools import reduce
reduce(function, iterable)
Example:
Python Code
from functools import reduce
# Example function to add two numbers
def add(x, y):
return x + y
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Applying reduce function
sum_of_numbers = reduce(add, numbers)
print(sum_of_numbers) # Output: 15
Using a lambda function:
Python Code
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers) # Output: 15
Practical Examples
Example 1: Using map to convert temperatures from Celsius to Fahrenheit
Python Code
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# List of temperatures in Celsius
celsius_temperatures = [0, 20, 30, 100]
# Applying map function
fahrenheit_temperatures = list(map(celsius_to_fahrenheit, celsius_temperatures))
print(fahrenheit_temperatures) # Output: [32.0, 68.0, 86.0, 212.0]
Example 2: Using filter to get prime numbers from a list
Python Code
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Applying filter function
prime_numbers = list(filter(is_prime, numbers))
print(prime_numbers) # Output: [2, 3, 5, 7]
Example 3: Using reduce to find the product of a list of numbers
Python Code
from functools import reduce
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Applying reduce function to find the product
product_of_numbers = reduce(lambda x, y: x * y, numbers)
print(product_of_numbers) # Output: 120
These examples demonstrate how map, filter, and reduce can be used to perform various operations on lists in a functional programming style.
Top of Form