To filter a list in Python, use the filter() method. The filter() is a built-in Python method that filters the list with the help of a function that tests each item in the list. If it passes the test, it will be included in the output; otherwise not.
Let’s see a scenario in which we have to filter a list containing positive integers such that the numbers are less than “7” and greater than “2”.
For better understanding,
Let the list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
So, according to the condition, the filtered list should be
[3, 4, 5, 6]
To achieve this filtered list in Python:
- Use filter() function
- Use the list comprehension
Using filter() function
The filter() function returns a list where the elements are filtered through a function to test if the element is accepted or not.
- function: It is a Function to be run for each item in the iterable.
- list: It is iterable to be filtered.
unfilteredList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filteredList = filter(lambda x: x < 7 and x > 2, unfilteredList) print(list(filteredList))
[3, 4, 5, 6]
In this example, first, we defined a list that we wanted to filter. Then, we used a filter() function, which accepts the lambda function and a list. Finally, the lambda function defined a filter in which we wrote a logic of which elements should stay in the list, and the second argument is a list.
Using list comprehension
List comprehension offers a shorter syntax to create lists based on existing iterables in Python. Therefore, list comprehension is better and more Pythonic way to achieve our goal.
STEP 1: Create a list
unfilteredList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
STEP 2: Use a list comprehension
List comprehension means creating a new list based on iterable values. It contains two parts, one is the expression, and another one is the context followed by the condition(optional).
listComprehension = [expression for item in iterable if condition == True]
See the following code.
filteredList = [x for x in unfilteredList if (x > 2 and x < 7)]
So, here “x” is the expression and the “for loop” in the context part with the “if” statement as the condition part.
STEP 3: Print the filtered list.
The print() is a built-in Python function that prints the specified message to the screen or another standard output device.
See the following complete code.
unfilteredList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filteredList = [x for x in unfilteredList if (x > 2 and x < 7)] print(filteredList)
[3, 4, 5, 6]
You can see that we got the filtered list according to our filter logic. And hence, list comprehension is the most potent way of coding, which allows us to write a code in just a few lines.
In this article, we learned two ways to filter a list, i.e.,
- Using List Comprehension
- Using “filter()” function.
That’s it for this tutorial.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.