Check if Value Exists in List of Dictionaries in Python (2 Examples)

 

Hello! In this tutorial, I will show you two methods of checking whether a value exists in a list of dictionaries in the Python programming language.

First, here is a quick overview of this tutorial:

Let’s dive into it!

 

Create List of Dictionaries

We will need to create a Python list of dictionaries. Therefore, run the line of code below in your Python IDE.

list_of_dicts = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]

 

Example 1: For Loop

In this example, we will create a custom function, in which we will write a for loop that will iterate through the list using the “if” and “else” statements, which returns the boolean value True or False depending on whether the value exists in the list or not.

list_of_dicts = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
value = 4
 
def value_exists(value, list_of_dicts):
    for dictionary in list_of_dicts:
        if value in dictionary.values():
            return True
    return False

Next, we will parse the value and list to the function, using again the “if” and “else” statements.

if value_exists(value, list_of_dicts):
    print(f"Value {value} exists.")
else:
    print(f"Value {value} doesn't exist.")
 
# Value 4 exists.

 

Example 2: Python’s any() Function

In this example, we will use Python’s built-in any() function, which is a simpler solution, to check if a value exists in the list of dictionaries. If it exists, it will return the boolean value True; but if it does not exist, it will return the boolean value False.

list_of_dicts = [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
value = 4
 
any(value in d.values() for d in list_of_dicts)
 
# True

Within the function any(), a list comprehension will loop through the list and check if the value of interest exists within the value of each dictionary in the list.

So, that’s how to check if a value exists in a list of dictionaries in Python. I hope you found this helpful.

 

Video, Further Resources & Summary

Do you need more explanations on how to check if a value 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 value exists in a list of dictionaries in Python.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to check if a value 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.

Menu
Top