How to Filter a Dictionary in Python

I was working on a data-cleaning project where I needed to filter a large Python dictionary based on specific conditions. The challenge was simple: I had a dictionary with hundreds of key-value pairs, but I only needed a subset that met certain criteria.

If you’ve ever worked with Python dictionaries, you know how powerful and flexible they are. However, filtering them efficiently can sometimes be tricky, especially if you’re new to Python or dealing with complex conditions.

In this article, I’ll show you five simple and practical ways to filter a dictionary in Python.
These methods will work for any dataset, whether you’re analyzing customer data, filtering product prices, or managing user information.

Understand Python Dictionaries

Before we start filtering, let’s quickly revisit what a Python dictionary is. A dictionary in Python is a collection of key-value pairs enclosed in curly braces {}.

Each key in the dictionary must be unique, and it maps to a specific value.
Here’s a quick example:

# Example of a Python dictionary
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

In this example, the keys represent employee names, and the values represent their annual salaries. Now, let’s explore different ways to filter this dictionary using Python.

Method 1 – Use Dictionary Comprehension

One of the most Pythonic ways to filter a dictionary is by using dictionary comprehension. This approach is clean, fast, and easy to read.

Suppose I want to filter out employees who earn more than $70,000 per year.
Here’s how I can do it:

# Filter dictionary using dictionary comprehension
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

# Filter employees with salary > 70000
filtered_data = {name: salary for name, salary in employee_data.items() if salary > 70000}

print(filtered_data)

Output:

{'John': 72000, 'Emma': 85000, 'Sophia': 91000}

I executed the above example code and added the screenshot below.

python filter dictionary

I often use it when I need to apply simple conditions to filter dictionaries in Python.

Method 2 – Use the filter() Function with Lambda

The filter() function in Python can also be used to filter dictionaries. It’s especially useful when you want a more functional programming approach.

Here’s how you can use it:

# Using filter() and lambda to filter dictionary
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

# Filter employees earning above 70000
filtered_data = dict(filter(lambda item: item[1] > 70000, employee_data.items()))

print(filtered_data)

Output:

{'John': 72000, 'Emma': 85000, 'Sophia': 91000}

I executed the above example code and added the screenshot below.

python filter dictionary by values

The filter() function returns an iterator, so I converted it back to a dictionary using dict().
This is a neat and efficient way to handle filtering when you prefer a functional style in Python.

Method 3 – Filter by Keys in Python

Sometimes you may want to filter a dictionary by specific keys rather than values.
For example, let’s say I only want data for certain employees.

Here’s how I can do that:

# Filter dictionary by keys
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

# Keep only selected employees
selected_names = ['Emma', 'Sophia']

filtered_data = {name: employee_data[name] for name in selected_names if name in employee_data}

print(filtered_data)

Output:

{'Emma': 85000, 'Sophia': 91000}

I executed the above example code and added the screenshot below.

python filter dict

Filtering by keys is very common when working with user-defined selections or data subsets. In my experience, this is one of the most practical ways to handle dictionary filtering in real-world Python projects.

Method 4 – Filter by Multiple Conditions

In real-life Python projects, you often need to apply multiple filtering conditions. For instance, you may want to filter employees who earn between $60,000 and $90,000.

Here’s how you can do that:

# Filter dictionary using multiple conditions
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

# Apply multiple conditions
filtered_data = {name: salary for name, salary in employee_data.items() if 60000 < salary < 90000}

print(filtered_data)

Output:

{'John': 72000, 'Emma': 85000, 'Michael': 64000}

I executed the above example code and added the screenshot below.

python dictionary comprehension filter

This method gives you more control over your filtering logic. It’s perfect when working with datasets that require complex conditions or thresholds.

Method 5 – Filter Nested Dictionaries in Python

In many Python applications, dictionaries are nested, meaning values themselves are dictionaries. Filtering such data requires a slightly different approach.

Let’s consider an example where each employee has more detailed information:

# Filtering nested dictionary
employees = {
    'John': {'age': 28, 'salary': 72000, 'city': 'New York'},
    'Emma': {'age': 34, 'salary': 85000, 'city': 'Chicago'},
    'Michael': {'age': 25, 'salary': 64000, 'city': 'Dallas'},
    'Sophia': {'age': 30, 'salary': 91000, 'city': 'San Francisco'},
    'David': {'age': 27, 'salary': 58000, 'city': 'Boston'}
}

# Filter employees with salary greater than 70000
filtered_employees = {name: info for name, info in employees.items() if info['salary'] > 70000}

print(filtered_employees)

Output:

{
    'John': {'age': 28, 'salary': 72000, 'city': 'New York'},
    'Emma': {'age': 34, 'salary': 85000, 'city': 'Chicago'},
    'Sophia': {'age': 30, 'salary': 91000, 'city': 'San Francisco'}
}

This approach is extremely useful when working with structured data, like JSON responses or API data. I use this technique often when dealing with employee or product datasets in Python.

Bonus Tip – Use pandas to Filter a Dictionary-Like Structure

Although not strictly a dictionary method, Python’s pandas library can handle dictionary-like data efficiently. If your data is large, converting it into a DataFrame makes filtering easier.

Here’s an example:

import pandas as pd

# Convert dictionary to DataFrame
employee_data = {
    'John': 72000,
    'Emma': 85000,
    'Michael': 64000,
    'Sophia': 91000,
    'David': 58000
}

df = pd.DataFrame(list(employee_data.items()), columns=['Name', 'Salary'])

# Filter employees with salary > 70000
filtered_df = df[df['Salary'] > 70000]

print(filtered_df)

Output:

      Name  Salary
0     John   72000
1     Emma   85000
3   Sophia   91000

Using pandas is ideal when you’re dealing with large datasets or need to perform advanced filtering operations. This approach combines the power of Python dictionaries and pandas’ analytical capabilities.

Common Mistakes When Filtering Dictionaries in Python

Over the years, I’ve seen developers make a few common mistakes when filtering dictionaries.
Here are some tips to avoid them:

  1. Forgetting to convert the result back to a dictionary – The filter() function returns an iterator, not a dictionary.
  2. Modifying the dictionary while iterating – Always create a new dictionary instead of altering the original one in a loop.
  3. Using incorrect conditions – Make sure your logical expressions are correct and handle edge cases (like missing keys).

By keeping these points in mind, you’ll write cleaner and more efficient Python code.

Real-Life Example: Filter Customer Orders

Let’s take a real-world example that’s more relatable for businesses in the USA. Imagine you have a dictionary of customer orders, and you want to filter out only those from California with an order value above $500.

Here’s how you can do it in Python:

# Filtering customer orders based on conditions
orders = {
    'Order001': {'state': 'California', 'amount': 750},
    'Order002': {'state': 'Texas', 'amount': 400},
    'Order003': {'state': 'California', 'amount': 1200},
    'Order004': {'state': 'New York', 'amount': 950},
    'Order005': {'state': 'California', 'amount': 300}
}

# Filter orders from California with amount > 500
filtered_orders = {
    order_id: details
    for order_id, details in orders.items()
    if details['state'] == 'California' and details['amount'] > 500
}

print(filtered_orders)

Output:

{
    'Order001': {'state': 'California', 'amount': 750},
    'Order003': {'state': 'California', 'amount': 1200}
}

This example shows how filtering dictionaries in Python can be applied in real-world business data scenarios. It’s efficient, readable, and works perfectly for conditional filtering.

Filtering a dictionary in Python is a skill every developer should master. Whether you’re working with small datasets or large-scale applications, these methods will save you time and effort.

We explored multiple approaches, from dictionary comprehensions and lambda functions to filtering nested dictionaries and using pandas. Each method has its strengths, and choosing one depends on your specific use case.

You may also like to 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.