How to Filter Lists in Python?

In this tutorial, I will explain how to effectively filter lists in Python based on specific conditions. Recently, in a Python webinar, Someone asked me a question on filtering lists. Then, I researched and explored more about this topic, and I will share my findings with suitable examples and screenshots.

Filter Lists in Python

Before getting into the different techniques, let’s understand the concept of list filtering. In Python, filtering a list means creating a new list that contains only the elements that satisfy a certain condition. This is particularly useful when working with large datasets or when you need to extract specific information from a list.

Read How to Sort a List of Objects by Attribute in Python?

1. Use a For Loop

One of the most simple methods to filter a list is by using a for loop. This approach involves iterating through each element in the list and appending the elements that meet the condition to a new Python list. Here’s an example:

# Original list of employees
employees = [
    {"name": "John Smith", "age": 35, "department": "Sales"},
    {"name": "Emily Johnson", "age": 28, "department": "Marketing"},
    {"name": "Michael Davis", "age": 42, "department": "Engineering"},
    {"name": "Sarah Wilson", "age": 31, "department": "Sales"}
]

# Filtering employees from the Sales department
sales_employees = []
for employee in employees:
    if employee["department"] == "Sales":
        sales_employees.append(employee)

print(sales_employees)

Output:

[{'name': 'John Smith', 'age': 35, 'department': 'Sales'}, {'name': 'Sarah Wilson', 'age': 31, 'department': 'Sales'}]

You can look at the screenshot below to see the output.

Filter Lists in Python

In this example, we have a list of employees with their names, ages, and departments. We want to filter out the employees who work in the Sales department. By using a for loop, we iterate through each employee and check if their department is “Sales”. If the condition is met, we append the employee to the sales_employees list.

Check out How to Print a List Without Brackets in Python?

2. Use List Comprehension for Concise Filtering

List comprehension is a more concise and Pythonic way to filter lists. It allows you to create a new list based on a condition in a single line of code. Here’s an example:

# Original list of customers
customers = [
    {"name": "Alice Thompson", "age": 29, "location": "New York"},
    {"name": "Bob Anderson", "age": 45, "location": "California"},
    {"name": "Carol Martinez", "age": 37, "location": "New York"},
    {"name": "David Harris", "age": 52, "location": "Texas"}
]

# Filtering customers from New York
new_york_customers = [customer for customer in customers if customer["location"] == "New York"]

print(new_york_customers)

Output:

[{'name': 'Alice Thompson', 'age': 29, 'location': 'New York'}, {'name': 'Carol Martinez', 'age': 37, 'location': 'New York'}]

You can look at the screenshot below to see the output.

How to Filter Lists in Python

In this example, we have a list of customers with their names, ages, and locations. We want to filter out the customers who are from New York. By using list comprehension, we create a new list new_york_customers that contains only the customers whose location is “New York”. This approach is more compact and readable compared to using a for loop.

Read How to Generate a List of Random Numbers in Python?

3. Use the filter() Function

Python provides a built-in filter() function that allows you to filter lists based on a function that returns a Boolean value. This function takes two arguments: the function that defines the filtering condition and the list you want to filter. Here’s an example:

# Original list of products
products = [
    {"name": "iPhone 13", "price": 999, "in_stock": True},
    {"name": "Samsung Galaxy S21", "price": 849, "in_stock": False},
    {"name": "Google Pixel 6", "price": 799, "in_stock": True},
    {"name": "OnePlus 9 Pro", "price": 969, "in_stock": False}
]

# Filtering in-stock products
in_stock_products = list(filter(lambda product: product["in_stock"], products))

print(in_stock_products)

Output:

[{'name': 'iPhone 13', 'price': 999, 'in_stock': True}, {'name': 'Google Pixel 6', 'price': 799, 'in_stock': True}]

You can look at the screenshot below to see the output.

Filter Lists in Python filter()

In this example, we have a list of products with their names, prices, and stock availability. We want to filter out the products that are currently in stock. By using the filter() function along with a lambda function, we create a new list in_stock_products that contains only the products whose in_stock value is True. The lambda function defines the condition for filtering, and the filter() function applies it to each element in the products list.

Check out How to Slice Lists in Python?

Advanced Filtering Techniques

In addition to the basic filtering methods, Python offers more advanced techniques for filtering lists. Here are a few examples:

1. Filter Based on Multiple Conditions

You can filter lists based on multiple conditions by combining them using logical operators such as and and or. Here’s an example:

# Original list of employees
employees = [
    {"name": "John Smith", "age": 35, "department": "Sales", "salary": 50000},
    {"name": "Emily Johnson", "age": 28, "department": "Marketing", "salary": 60000},
    {"name": "Michael Davis", "age": 42, "department": "Engineering", "salary": 80000},
    {"name": "Sarah Wilson", "age": 31, "department": "Sales", "salary": 55000}
]

# Filtering employees based on department and salary
high_paid_sales_employees = [employee for employee in employees if employee["department"] == "Sales" and employee["salary"] > 52000]

print(high_paid_sales_employees)

In this example, we filter the list of employees to find those who work in the Sales department and have a salary greater than $52,000. By combining the conditions using the and operator, we can create more specific filtering criteria.

Read How to Concatenate a List of Strings into a Single String in Python?

2. Filter Nested Lists

Sometimes, you may encounter nested lists where you need to filter elements based on conditions within the nested structure. Here’s an example:

# Original list of orders
orders = [
    {"customer": "Alice Thompson", "items": [{"name": "Shirt", "price": 25}, {"name": "Pants", "price": 40}]},
    {"customer": "Bob Anderson", "items": [{"name": "Shoes", "price": 80}, {"name": "Socks", "price": 10}]},
    {"customer": "Carol Martinez", "items": [{"name": "Dress", "price": 60}, {"name": "Jacket", "price": 90}]}
]

# Filtering orders with items over $50
expensive_orders = [order for order in orders if any(item["price"] > 50 for item in order["items"])]

print(expensive_orders)

In this example, we have a list of orders, where each order contains a customer name and a list of items. We want to filter out the orders that have at least one item priced over $50. By using a nested list comprehension with the any() function, we can check if any item in the order meets the condition and include the entire order in the expensive_orders list.

Check out How to Iterate Through a List Backward in Python?

Conclusion

In this article, I explained how to filter lists in Python. I explained three methods, such as using a for loop, List comprehension for concise filtering, using the filter() function. I also covered advanced filtering techniques, such as filtering based on multiple conditions and filtering nested lists.

You can read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.