Summary: in this tutorial, youll learn how to filter list elements by using the built-in Python filter[] function.
Introduction to Python filter[] function
Sometimes, you need to iterate over elements of a list and select some of them based on specified criteria.
Suppose that you have the following list of scores:
To get all elements from the scores list where each element is greater than or equal to 70, you use the following code:
How it works.
- First, define an empty list [filtered] that will hold the elements from the scores list.
- Second, iterate over the elements of the scores list. If the element is greater than or equal to 70, add it to the filtered list.
- Third, show the filtered list to the screen.
Python has a built-in function called filter[] that allows you to filter a list [or a tuple] in a more beautiful way.
The following shows the syntax of the filter[] function:
The filter[] function iterates over the elements of the list and applies the fn[] function to each element. It returns an iterator for the elements where the fn[] returns True.
In fact, you can pass any iterable to the second argument of the filter[] function, not just a list.
The following shows how to use the filter[] function to return a list of scores where each score is greater than or equal to 70:
Output:
Since the filter[] function returns an iterator, you can use a for loop to iterate over it. Or you can use the list[] function to convert the iterator to a list.
Using the Python filter[] function to filter a list of tuples example
Suppose you have the following list of tuples:
Each element in a list is a tuple that contains the country name and population.
To get all the countries whose populations are greater than 300 million, you can use the filter[] function as follows:
Output:
Summary
- Use the Python filter[] function to filter a list [or a tuple].