Check If Key Exists in List of Dictionaries in Python (3 Examples)

 

Hi! In this tutorial, you’ll learn 3 ways of checking if a key exists in a list of dictionaries in the Python programming language.

First, here is a quick overview of this tutorial:

Let’s get right into it!

 

Create List of Dictionaries

First of all, we will create a list of dictionaries. To do so, run the following line of code in your Python programming IDE.

dict_list = [{'a': 1}, {'b': 2}, {'c': 3}]

 

Example 1: For Loop

In this first example, we are going to build a custom Python function, which will comprise a for loop that will iterate through the list of dictionaries, and return the boolean True or False, if the key either exists in the list of dictionaries or does not exist in the list of dictionaries:

key = 'a'
 
def key_exists(key, list_of_dicts):
    for i in list_of_dicts:
        if key in i:
            return True
        else:
            return False

Next, we will apply the function to the list of dictionaries, using the “if” and “else” statement like so:

if key_exists(key, dict_list):
    print(f'{key} exists.')
else:
    print(f"{key} doesn't exist.")
 
# a exists.

Example 2: Python’s any() Function

In this example, we will make use of Python’s any() function to check if the key exists in the list of dictionaries:

key = 'a'
 
if any(key in d for d in dict_list):
    print(f'{key} exists.')
else:
    print(f"{key} doesn't exist.")
 
# a exists.

 

Example 3: List Comprehension

In this last example, we will make use of Python’s list comprehension method to return the dictionaries in which a particular key exists. We will create a new list of dictionaries:

my_list = [{'a': 1}, {'b': 2}, {'c': 3},{'a': 10}]
key = 'a'

Next, we will use list comprehension method to extract the dictionaries in which the key exists:

result = [i for i in my_list if key in i ]
print(result)
 
# [{'a': 1}, {'a': 10}]

So, that is how to check if a key exists in a list of dictionaries in the Python programming language. I hope you found this helpful.
 

Video, Further Resources & Summary

Do you need more explanations on how to check if a key exists in a list of dictionaries in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to check if a key exists in a list of dictionaries in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python lists tutorials on Statistics Globe, starting with these ones:

This post has shown how to check if a key exists in a list of dictionaries in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top