How to Filter a List of Dictionaries in Python?

1.7/5 - (3 votes)

In this article, you’ll learn how to filter a list of dictionaries. So let’s get started!

Short answer: The list comprehension statement [x for x in lst if condition(x)] creates a new list of dictionaries that meet the condition. All dictionaries in lst that don’t meet the condition are filtered out. You can define your own condition on list element x.

Here’s a quick and minimal example:

l = [{'key':10}, {'key':4}, {'key':8}]

def condition(dic):
    ''' Define your own condition here'''
    return dic['key'] > 7

filtered = [d for d in l if condition(d)]

print(filtered)
# [{'key': 10}, {'key': 8}]

Try it yourself in the interactive Python shell (in your browser):

You’ll now get the step-by-step solution of this solution. I tried to keep it as simple as possible. So keep reading!

Filter a List of Dictionaries By Value

Problem: Given a list of dictionaries. Each dictionary consists of one or more (key, value) pairs. You want to filter them by value of a particular dictionary key (attribute). How do you do this?

Minimal Example: Consider the following example where you’ve three user dictionaries with username, age, and play_time keys. You want to get a list of all users that meet a certain condition such as play_time>100. Here’s what you try to accomplish:

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

print(superplayers)

The output should look like this where the play_time attribute determines whether a dictionary passes the filter or not, 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 that meet the condition. All dictionaries in lst that don’t meet the condition are filtered out. You can define your own condition on list element x.

Here’s the code that shows you how to filter out all user dictionaries that don’t meet the condition of having played at least 100 hours.

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]

print(superplayers)

The output is the filtered list of dictionaries that meet the condition:

[{'username': 'alice', 'age': 23, 'play_time': 101},
 {'username': 'ann', 'age': 25, 'play_time': 121}]

Try It Yourself:

Related articles on the Finxter blog:

Filter a List of Dictionaries By Key

Problem: Given a list of dictionaries. Each dictionary consists of one or more (key, value) pairs. You want to filter them by key (attribute). All dictionaries that don’t have this key (attribute) should be filtered out. How do you do this?

Minimal Example: Consider the following example again where you’ve three user dictionaries with username, age, and play_time keys. You want to get a list of all users for which the key play_time exists. Here’s what you try to accomplish:

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

superplayers = # Filtering Magic Here

print(superplayers)

The output should look like this where the play_time attribute determines whether a dictionary passes the filter or not (as long as it exists, it shall pass the filter).

[{'username': 'alice', 'age': 23, 'play_time': 101},
 {'username': 'bob', 'age': 31, 'play_time': 88}]

Solution: Use list comprehension [x for x in lst if condition(x)] to create a new list of dictionaries that meet the condition. All dictionaries in lst that don’t meet the condition are filtered out. You can define your own condition on list element x.

Here’s the code that shows you how to filter out all user dictionaries that don’t meet the condition of having a key play_time.

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

superplayers = [user for user in users if 'play_time' in user]

print(superplayers)

The output is the filtered list of dictionaries that meet the condition:

[{'username': 'alice', 'age': 23, 'play_time': 101},
 {'username': 'bob', 'age': 31, 'play_time': 88}]

Try It Yourself:

Related articles on the Finxter blog:

Related Question: How to Sort a List of Dictionaries By Key Value

Original article

Python – How to Sort a List of Dictionaries?

Problem: Given a list of dictionaries. Each dictionary consists of multiple (key, value) pairs. You want to sort them by value of a particular dictionary key (attribute). How do you sort this dictionary?

Minimal Example: Consider the following example where you want to sort a list of salary dictionaries by value of the key 'Alice'.

salaries = [{'Alice': 100000, 'Bob': 24000},
            {'Alice': 121000, 'Bob': 48000},
            {'Alice': 12000, 'Bob': 66000}]

sorted_salaries = # ... Sorting Magic Here ...

print(sorted_salaries)

The output should look like this where the salary of Alice determines the order of the dictionaries:

[{'Alice': 12000, 'Bob': 66000},
{'Alice': 100000, 'Bob': 24000},
{'Alice': 121000, 'Bob': 48000}]

Solution: You have two main ways to do this—both are based on defining the key function of Python’s sorting methods. The key function maps each list element (in our case a dictionary) to a single value that can be used as the basis of comparison.

  • Use a lambda function as key function to sort the list of dictionaries.
  • Use the itemgetter function as key function to sort the list of dictionaries.

Here’s the code of the first option using a lambda function that returns the value of the key 'Alice' from each dictionary:

# Create the dictionary of Bob's and Alice's salary data
salaries = [{'Alice': 100000, 'Bob': 24000},
            {'Alice': 121000, 'Bob': 48000},
            {'Alice': 12000, 'Bob': 66000}]

# Use the sorted() function with key argument to create a new dic.
# Each dictionary list element is "reduced" to the value stored for key 'Alice'
sorted_salaries = sorted(salaries, key=lambda d: d['Alice'])

# Print everything to the shell
print(sorted_salaries)

The output is the sorted dictionary. Note that the first dictionary has the smallest salary of Alice and the third dictionary has the largest salary of Alice.

[{'Alice': 12000, 'Bob': 66000},
 {'Alice': 100000, 'Bob': 24000},
 {'Alice': 121000, 'Bob': 48000}]

Try It Yourself:

Related articles:

Where to Go From Here

In this article, you’ve learned how to filter a list of dictionaries easily with a simple list comprehension statement. That’s far more efficient than using the filter() method proposed in many other blog tutorials. Guido, the creator of Python, hated the filter() function!

I’ve realized that professional coders tend to use dictionaries more often than beginners due to their superior understanding of the benefits of dictionaries. If you want to learn about those, check out my in-depth tutorial of Python dictionaries.

If you want to stop learning and start earning with Python, check out my free webinar “How to Become a Python Freelance Developer?”. It’s a great way of starting your thriving coding business online.

“[Webinar] How to Become a Python Freelance Developer?”