How to Filter List in Python

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:

  1. Use filter() function
  2. 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.

Syntax

filter(function, list)

Arguments

  1. function: It is a Function to be run for each item in the iterable.
  2. list: It is iterable to be filtered.

Example

unfilteredList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filteredList = filter(lambda x: x < 7 and x > 2, unfilteredList)
print(list(filteredList))

Output

[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).

Syntax

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.

print(filteredList)

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)

Output

[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.

Conclusion

In this article, we learned two ways to filter a list, i.e.,

  1. Using List Comprehension
  2. Using “filter()” function.

That’s it for this tutorial.

Related posts

How to Sort a List of Lists in Python

How to list files in directory in Python

Leave a Comment