8 Best Methods To Use Python Filter List

Hoe to Use Python Filter List | Optymize



This article will guide you with a python filter list. We will learn different methods you can use to filter lists in python. So, let’s begin.

The world today is running on technology. Everyday new software and applications are launched and a new piece of tech gets created. In all such technological developments, one language that is used to build almost every new piece of software is Python. Python is one of the most popular backend languages. It is a versatile language with its usage from web development to AI and Machine learning. 

In order to start a career in any software development field, Python is a must have language to learn. It has a simple syntax and a huge set of libraries and frameworks that helps write concise code and develop programs rapidly. Without further ado, let’s start with our topic of python filter list, what they are and ways to implement it.

What is a List?

A list is a type of data structure which can store a wide range of data in a single variable. It is defined in the form of

[ a,b,c,d,e ]

Its values are separated by commas and are defined inside square brackets[]. The values(a,b.c.d.e) can be of any data type- integer, float, string, etc. The list can be modified: any element at any index can be replaced, new elements can be inserted, and elements can be deleted as well. It suggests that A python list is a mutable data type.

We can use indexing to iterate between the list and can perform operations such as slicing.

Related Post: 5 Best Ways for Python Reverse String with Examples

How to Filter a List in Python?

We have an in-built Python filter list function that filters the given iterable set. It also uses a function that tests whether each element in the set is True or False. This function comes handy when we have to iterate a large set of elements or have a specific criteria to differentiate elements. Thus, it helps in filtering out unwanted values and keeps only those values that are needed in the result as per the given conditions. 

Here is the syntax of filter() function:


  1. Function: User-defined set of rules that activates when a particular function is called.
  2. Sequence: Set of lists that need to be filtered.

The filter() function is more efficient than the for-loop. That is because the function returns an iterator. That iterator is a filter object that yields values on demand and promotes an evaluation strategy. This return of the iterator makes the filter() function more memory efficient than the for loop. 

Another feature of the filter() function is that it removes outliers that affect the mean accuracy of data from the data set. Outliers are those “odd one out” data points that are significantly different from the whole normally distributed sample.

You can also hire experienced Python developers to seamlessly use Python Filter List for your complex projects.

Python Filter List - 8 Methods To Filter A List in Python

We have many methods in python to filter a list as per the requirements. Let’s learn about each of them with examples. 

1. Python Filter List of Objects

To filter list of objects, Python has a built-in function- filter() that can be used with lambda. We will learn about the lambda function below. The lambda function accesses attributes on the object and checks for a condition. 

Another way, which we will learn below is to use list comprehension to iterate the objects list. It also accesses an attribute and checks if it meets the certain condition. Then, the new list will only contain the values that meet the condition.

Python filter list of objects - Example

Filter list of objects with condition :

class Employee():

   def __init__(self, name, salary):
       self.name = name
       self.salary = salary

   def __repr__(self):
       return self.name

alice = Employee('John', 100)
bob = Employee('Mike', 70)
carl = Employee('Carl', 150)

list_of_objects = [alice, bob, carl]

filtered_list = list(
       lambda obj: obj.salary > 80,
[John, Carl]


[John, Carl]

2. Python Filter List By Condition

With this method, you can define any condition on elements in the list to filter them out. For that, You need to create a function (e.g., condition(x)) that takes one element from the list as input and returns the Boolean value True if the condition is met or False.

Python Filter List By Condition- Example

def condition(x):
    '''Define your arbitrarily
    complicated condition here'''

    return x<10 and x>0

lst = [11, 14, 3, 0, -1, -3]

# Filter out all elements that do
# not meet condition
filtered = [x for x in lst if condition(x)]
# [3]

Condition– All elements that are larger than 0 and smaller than 10 are included in the filtered list. 

Hence, only element 3 remains.

Related Post: Tutorial: How to Use Python Delete File

3. Python Filter List of Strings

Let’s say you have a list of strings and a query string. You have to filter those elements that contain the query string. How will you do that?

Take for example- You have a list [‘Adam’,’Ann’,’Anne’,’Jerry’] and you want to obtain the elements with the substring ‘Ann’.  

To achieve this, use the list comprehension method:  [x for x in list if condition] to filter out all elements that do not contain the desired string.

Python Filter List of Strings- Code Example

import re

# Define the list
customers = ['Adam', 'Ann', 'Anne', 'Jerry']

# Filter out all elements that contain 'An'
filtered = [x for x in customers if 'An' in x]

['Ann', 'Anne']

4. Python Filter List of Strings Using Another List

Now we will filter the data in a strings list without using any method. To filter, we will use another list. To achieve that, 

>we will declare two list variables- list1 and list2. 
>Then we will filter the values of list2 by the values of list1. 
>The script will match each value of list2 as per the condition with the values of list1
>Then it will print those values that don’t exist in list1.
Python Filter List of Strings Using Another List- Code Example

# Declare two list variables

list1 = ['Perl', 'PHP', 'Java', 'ASP']
list2 = ['JavaScript is client-side scripting language',
'PHP is a server-side scripting language',
'Java is a programming language',
'Bash is a scripting language']

# Filter the second list based on first list
filter_data = [x for x in list2 if
all(y not in x for y in list1)]

# Print list data before filter and after filter

print("The content of the first list:", list1)
print("The content of the second list:", list2)
print("The content of the second list after filter:", filter_data)

List1 did not contain ‘Bash’. Hence, the output will only contain the value of list2 with the word ‘Bash’ in it.  That is:
‘Bash is a scripting language’.

The content of the first list: [ ‘Perl’, ‘PHP’, ‘Java’, ‘ASP’ ]
The content of the second list:
['JavaScript is client-side scripting language', 
'PHP is a server-side scripting language', 
'Java is a programming language', 'Bash is a scripting language']
The content of the second list after filter:
['Bash is a scripting language']

5. Python Filter() function

We have discussed this function earlier. The filter() function accepts two parameters. The first list parameter will take a function name. The second list will take those names as values. filter() method will then store that data and check if it’s true or not. If not, it will discard the data. All false values will be the filtered data.

Related Post: Python Install Mac Tutorial

Python Filter() function- Example

We have a data set of class 10th students of XYZ public school. We want to filter those students whose percentage is above 80% and print their scores.

numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94]
 #function to check scores above 80
def check_score(number):
    if number >=80:
          return True  

    return False

# Extract elements from the numbers list for which check_score() returns True
#using filter function on list numbers to extract scores above 80
percentage_score = filter(check_score, numbers)

# converting to list
scores = list(percentage_score)
[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

As I have said above, the filter() method returns an iterator. We can either use a for loop or the list() function to convert the iterator to a list.

for i in percentage_score:
[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

6. Python Filter List lambda() Function

The filter() method is often used for lambda functions but it can also be used for nested lists. It is used to separate sets, lists or tuples. If the function returns True, the values will be filtered. 

Python Filter List lambda() Function: Example

Filter and print Prime numbers from a list of numbers.

numbers = [1, 2, 3, 4, 5, 6, 7, 9 ,11 , 10 , 15]

# the lambda function returns True for prime numbers 
prime_numbers = filter(lambda number: all( number%i != 0 for i in range(2, int(number**.5)+1) ), numbers)

# converting to list
prime_number = list(prime_numbers)



[1, 2, 3, 5, 7, 11]

7. Python Filter List Comprehension

List comprehension is one of the most used methods to filter a list. It is an efficient choice while using if-else conditions and for-loop on a list. You can use multiple such conditions with ease. It is a clean method with a short syntax of single lines of code. You can create or filter a new list as per the values of a previous list with this function.

Related Post: How to Represent Python Infinity in Simple Ways?

Python Filter List Comprehension- Example

Let’s perform the example we used in filter() function with List comprehension. Filter the students who scored more than 80% and print the score.

numbers = [70, 60, 80, 90, 50,82,90,91,84,82,94,99,78,65,61,45,89,87,49,76,81,94]
#function to check scores above 80
filtered_list = [ i for i in numbers if i>= 80]
[80, 90, 82, 90, 91, 84, 82, 94, 99, 89, 87, 81, 94]

8. Python Filter List of Dictionaries

A dictionary contains one or more (key, value) pairs. You have to use the Python filter list function to filter the dictionaries by the value of the dictionary key(attribute). How to do this?

Python Filter List of Dictionaries: Example

Let’s say you have three user dictionaries- username, age, and play_time keys. Get a list of all users with the condition play_time>100.

users = [{'username': 'alice', 'age': 23, 'play_time': 101},
         {'username': 'bob', 'age': 31, 'play_time': 88},
         {'username': 'ann', 'age': 25, 'play_time': 121},]

superplayers = # Filtering Magic Here


Expected Output:  The play_time attribute should filter the dictionaries, i.e., play_time>100:

[{'username': 'alice', 'age': 23, 'play_time': 101},
 {'username': 'ann', 'age': 25, 'play_time': 121}]
Solution: Use list comprehension [ x for x in lst if condition(x)] to create a new list of dictionaries for the given condition. Dictionaries that don’t meet the condition will then be filtered out. You can define your own condition on list element x.
users = [{'username': 'alice', 'age': 23, 'play_time': 101},
         {'username': 'bob', 'age': 31, 'play_time': 88},
         {'username': 'ann', 'age': 25, 'play_time': 121},]

superplayers = [user for user in users if user['play_time']>100]
[{'username': 'alice', 'age': 23, 'play_time': 101},
{'username': 'ann', 'age': 25, 'play_time': 121}]


With a Python filter list, we can filter a set of lists with different methods in an efficient manner. The filtering operation applies Boolean function to the values in an iterable and filters the values that return False. 

I hope with this guide you can now filter a list in Python with ease.  The Python filter() function helps code in an effortless manner due to its concise syntax. 



Subscribe for newsletter

Trusted By

Our platform focuses on making the remote hiring process easier by providing top quality vetted developers from around the world. Through our service many well-known companies have scaled their product development team.

Related Article

Ready to scale your team with remote engineers?