Filtering and Reducing Lists in Python
Python provides great capabilities to filter and reduce lists. In this article we'll explore how to filter elements from a list, transform values, reduce lists to a single value and write clean and expressive data transformations.
filter
In Python, you can filter iterables (like lists, tuples, or sets) using the built-in filter() function. Let's take a look at the following example.
numbers = [3, 7, 9, 11, 17, 24]
def is_greater_than_ten(x):
return x > 10
filtered = filter(is_greater_than_ten, numbers)
filtered_list = list(filtered)
The filter() function accepts as the first parameter a function that retrieves a boolean value. This way it can filter the value when the condition evaluates to True. The second parameter is the list to be filtered. Another thing to notice is that the filter function retrieves an iterator. If we want a list instead, we have to to use list().
In several cases, you don't want to define a new function to filter a list. We can rewrite the above expression and use a lambda function instead.
filtered = filter(lambda x: x > 10, numbers)
map
Mapping means applying a transformation to every element. The following example will transform the list and calculate the square for every item.
numbers = [1, 2, 3, 4]
squares = map(lambda x: x * x, numbers)
print(list(squares))
Just like filter(), the map() function returns an iterator.
List comprehensions
List comprehensions provide a concise and efficient way to create lists in Python. They replace more verbose for loops and map() or filter() functions, improving readability and performance for simple operations.
The basic syntax of a list comprehension is:
new_list = [expression for item in iterable if condition]
The following expression creates a list with squares for the first 5 integer numbers starting from zero.
squares = [x * x for x in range(5)];
# [0, 1, 4, 9, 16] The following example uses a condition to generate a list with the first 5 even numbers starting from zero.
evens = [x for x in range(10) if x % 2 == 0];
Reducing Lists
Reducing means combining all elements into a single value. In Python, reduce() is not built-in, it lives in functools. The following example uses reduce() to calculate the sum of all items in a list.
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, numbers, 0)
The first parameter of reduce() is a function that takes two arguments (accumulator and each item). The second parameter is the iterable and the last one is the initializer.