Back to articles list Articles
8 minutes read

How to Filter a Python Dictionary

Learn how to build your own filters with multiple conditions, then use them to filter Python dictionaries by any combination of keys and values.

Python dictionaries are extremely versatile. They can hold a variety of data types and are used to model many different real-world objects and relations. It is no surprise, then, that we want to perform specific operations (like filtering) on Python dictionaries. Filtering a dictionary in Python can be especially tricky because they can be filtered by key, values, or both, using an arbitrary number of conditions.

In this article, we will learn how to properly use filters on Python dictionaries. We’ll start by reviewing the basics of the Python dictionary. Then we’ll move on to the filter() function and how it can be applied to dictionaries.

For this article, you’ll need a basic understanding of Python data structures like dictionaries and sequences. If that’s not the case, fear not: we have a course on Python data structures in practice to help get you up to speed in no time.

How Does a Python Dictionary Work?

A Python dictionary works by creating associations between pairs of keys and values. Each key has a corresponding value associated with it. A dictionary can hold as many key-value pairs as desired.

Here’s a simple dictionary in Python:

grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

print(grades['John'])

# output:
# 7.8

Dictionaries allow us to create associations in the code that resemble how data is organized in the real world. In the example above, the grades dictionary contains keys (the student’s names) associated with values (each student’s corresponding grade). We can use a particular key, like 'John', to access its corresponding value (7.8 in this case).

That’s just a quick recap of Python dictionaries. If you want to learn more about them, we have articles on how to use Python dictionaries and on five ways to create them, so be sure to check them out. Now, let’s move on to filtering these dictionaries!

How to Filter with Python: The filter() Function

Python’s built-in filter() function can be used to filter different types of sequences and containers, including Python dictionaries. The filter() function takes two arguments: the first is a filtering function that “decides'' whether each element should be filtered or not. The second is the container whose elements will be filtered.

In order for the filter to work, the filtering function needs to take a value as its argument and return True or False (depending on whether the value should be filtered or not). In most cases, we write the filtering function ourselves. If you are not confident in creating your own functions, read this article on how to define a function in Python.

In the example below, we use filter() to eliminate negative values from the numbers list. To do that, we define the is_not_negative function to serve as our filtering function:

def is_not_negative(number):
	return number >= 0


numbers = [-1, 10, 56, -5, 0]

my_filter = filter(is_not_negative, numbers)

print(my_filter)

# output:
# <filter at 0x7fe451782520>

Wait, where’s our filtered results? As it turns out, my_filter isn’t exactly our filtered results just yet. filter() is a lazy function, which means that it computes the result only when we explicitly tell Python to do so. One way to do that is to cast the filter back to a sequence, such as a list:

def is_not_negative(number):
	return number >= 0


numbers = [-1, 10, 56, -5, 0]

my_filter = filter(is_not_negative, numbers)
result = list(my_filter)

print(result)

# output:
# [10, 56, 0]

Our filter works! The result list contains only the non-negative numbers of the original list.

A more advanced method for filtering Python lists is to use a list comprehension. Read this article on list comprehensions to learn more about it!

Use filter() to Filter Dictionaries in Python

We can apply the same basic logic in order to filter dictionaries in Python. There are only a few differences from the example in the previous section:

  • Instead of elements in a list, we need to iterate over the key-value pairs of the dictionary. We can do this by using the dictionary dict.items() method.
  • Our filtering function needs to be adapted to deal with the keys and/or values of the dictionary. The specifics depend on exactly how we want to perform our filtering.
  • We use the dict() function instead of list() to cast the filter back into a dictionary (since we are not dealing with a list anymore).

Adjusting to all of these changes, the code will look something like this:

def my_filtering_function(pair):
    pass


grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

filtered_grades = dict(filter(my_filtering_function, grades.items()))

We’re almost there – we haven’t implemented my_filtering_function just yet, so the resulting dictionary in filtered_grades is empty. Let’s discuss some filtering strategies that we can use.

Filter a Key Out of a Python Dictionary

A common operation is to filter an unwanted key out of the dictionary. To achieve this in our filtering function, we can check if the given key is wanted or not and then return True or False accordingly.

We do precisely that in the example below:

def my_filtering_function(pair):
    unwanted_key = 'Matt'
    key, value = pair
    if key == unwanted_key:
        return False  # filter pair out of the dictionary
    else:
        return True  # keep pair in the filtered dictionary


grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

filtered_grades = dict(filter(my_filtering_function, grades.items()))

print(filtered_grades)

# output:
# {'John': 7.8, 'Mary': 9.0, Michael': 9.5}

Success! We have filtered the unwanted key 'Matt' (and its associated value) out of the dictionary. Note that we had to “break up” the pair into the key and value so that we have access to the key for the filtering logic.

Filter Multiple Keys Out of a Python Dictionary

From this point onwards, you only need to adapt the logic in my_filtering_function to change how the filtering is performed. For example, we can use a list of wanted or unwanted keys to filter out multiple keys at once:

def my_filtering_function(pair):
    wanted_keys = ['John', 'Matt']
    key, value = pair
    if key in wanted_keys:
        return True  # keep pair in the filtered dictionary
    else:
        return False  # filter pair out of the dictionary


grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

filtered_grades = dict(filter(my_filtering_function, grades.items()))

print(filtered_grades)

# output:
# {'John': 7.8, 'Matt': 8.6}

In this case, only keys inside the wanted_keys list appear in the filtered dictionary.

Filter a Python Dictionary by Value

Did you notice that we still have not used the value variable inside the filtering function? If we want to filter a Python dictionary by value, we simply need to use this variable at any point in our filtering logic. For example:

def my_filtering_function(pair):
    key, value = pair
    if value >= 8.5:
        return True  # keep pair in the filtered dictionary
    else:
        return False  # filter pair out of the dictionary

grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

filtered_grades = dict(filter(my_filtering_function, grades.items()))

print(filtered_grades)

# output:
# {'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

Here, we filtered the grades dictionary by its values. Only grades of 8.5 and higher appear in the filtered dictionary.

Filter a Python Dictionary by Multiple Conditions

Naturally, you can combine filtering by keys and values however you want. No matter the complexity, any filtering logic can be used to filter a Python dictionary, as long as it eventually returns True or False.

In the example below, we filter the grades dictionary by all of the following conditions:

  • The student’s name must have 4 characters.
  • The student’s name must start with the letter “M”.
  • The student’s grade must be above 8.9.

Note how, despite its apparent complexity, it is quite simple to test each one of these conditions. If one of these conditions is not met, the filtering function returns False. Otherwise, the function moves on to the next condition. Finally, if all conditions are met, the function returns True.

Here’s how it looks in action:

def my_filtering_function(pair):
    key, value = pair
    if len(key) != 4:  # first condition
        return False
    if key[0] != 'M':
        return False  # second condition
    if value <= 8.9:
        return False  # third condition
    # If nothing was returned until here, it means that
    # the pair passed all conditions. Keep it in the dictionary!
    return True

grades = {'John': 7.8, 'Mary': 9.0, 'Matt': 8.6, 'Michael': 9.5}

filtered_grades = dict(filter(my_filtering_function, grades.items()))

print(filtered_grades)

# output:
# {'Mary': 9.0}

And there it is: even though each one of the other students fit some of the conditions, only 'Mary' was able to satisfy all conditions simultaneously. Talk about a powerful filter!

Keep Your Python Skills Going Strong

We have reached the end of the article, but don’t stop learning here! If you really want to sink your teeth into processing different file formats with Python, head over to our Data Processing with Python track. Alternatively, you can learn more about the filter() function – along with the map() and reduce() functions – in this article about working with streams in Python. Happy learning!