Python: Check if value exists in list of dictionaries

In this article we will discuss different ways to check if a given value exists in a list of dictionaries or not.

Table of Contents:

Suppose we have a list of a dictionary,

# List of dictionaries
list_of_dict = [
    {'Name': 'Shaun', 'Age': 35, 'City': 'Delhi'},
    {'Name': 'Ritika', 'Age': 31, 'City': 'Mumbai', 'Country': 'India'},
    {'Name': 'Smriti', 'Age': 33, 'City': 'Sydney', 'Country': 'India'},
    {'Name': 'Jacob', 'Age': 23, 'City': 'Tokyo'},
]

This list contains four different dictionaries, which internally contains various key-value pairs. Now we want to check if a given value exists in any of the dictionaries in this list of dictionaries or not. There are different ways to do that, let’s discuss them one by one.

Get all values in a list of dictionary & check if a given value exists

We can iterate over all the key-value pairs in all the dictionaries in the list and then create a list of all values in these dictionaries. Then we can check if our given value exists in this list of values or not,

# List of dictionaries
list_of_dict = [
    {'Name': 'Shaun', 'Age': 35, 'City': 'Delhi'},
    {'Name': 'Ritika', 'Age': 31, 'City': 'Mumbai', 'Country': 'India'},
    {'Name': 'Smriti', 'Age': 33, 'City': 'Sydney', 'Country': 'India'},
    {'Name': 'Jacob', 'Age': 23, 'City': 'Tokyo'},
]

# Create a list of all values in list of dictionaries
list_of_all_values = [value for elem in list_of_dict
                      for value in elem.values()]

# Value to be checked
value = 'Sydney'

# check if given value exists in list of values from list of dictionaries
if value in list_of_all_values:
    print(f"Yes, Value: '{value}' exists in list of dictionaries")
else:
    print(f"No, Value: '{value}' does not exists in list of dictionaries")

Output:

Yes, Value: 'Sydney' exists in list of dictionaries

In the above example, using list comprehension, we iterated over all the dictionaries in a list and for each dictionary we iterated over all the values in it and appended all the values to a new list. Then we checked if our given value exists on that list or not.

Iterate over all the dicts in a list of dicts & check if a given value exists

In the previous example, we iterated over all the key-value pairs of all dictionaries and built a list of values, then we looked for the given value in that list. But that seems an inefficient solution, because we are iterated over all the contents of dictionaries and then searching for value. Instead we should keep looking for value during the iteration and as soon as the value is found we should stop the remaining iteration. For example,

# List of dictionaries
list_of_dict = [
    {'Name': 'Shaun', 'Age': 35, 'City': 'Delhi'},
    {'Name': 'Ritika', 'Age': 31, 'City': 'Mumbai', 'Country': 'India'},
    {'Name': 'Smriti', 'Age': 33, 'City': 'Sydney', 'Country': 'India'},
    {'Name': 'Jacob', 'Age': 23, 'City': 'Tokyo'},
]

def check_if_in_list_of_dict(sample_dict, value):
    """Check if given value exists in list of dictionaries """
    for elem in sample_dict:
        if value in elem.values():
            return True
    return False

value = 'Sydney'

if check_if_in_list_of_dict(list_of_dict, value):
    print(f"Yes, Value: '{value}' exists in list of dictionaries")
else:
    print(f"No, Value: '{value}' does not exists in list of dictionaries")

Output:

Yes, Value: 'Sydney' exists in list of dictionaries

In the above example, we iterated over all the dictionaries in the list of dictionaries using for a loop. During iteration for each dictionary we checked in the given value exists in the values of this dictionary or not. As soon as we found a dictionary that contains the given value, we stopped the remaining iteration.

Use any() & List comprehension to check if a value exists in a list of dictionaries

Using list comprehension we will create a bool list. For this we will iterate over all the dictionaries in a list of dictionaries and for each dictionary we will check if a given value exists in that dictionary or not. If yes then add True in the bool list. Once list comprehension is over, then it will return a bool list and the number of True elements in that bool list represents the number of times given value found in a list of dictionaries. Then we can call the any() function on this bool list to check if the list contains any True element or not. If yes then it means value exists in a list of dictionaries. For example,

# List of dictionaries
list_of_dict = [
    {'Name': 'Shaun', 'Age': 35, 'City': 'Delhi'},
    {'Name': 'Ritika', 'Age': 31, 'City': 'Mumbai', 'Country': 'India'},
    {'Name': 'Smriti', 'Age': 33, 'City': 'Sydney', 'Country': 'India'},
    {'Name': 'Jacob', 'Age': 23, 'City': 'Tokyo'},
]


# Create a bool list, number of True in list represents the
# number of dictionaries that contains the given value
list_of_bool = [True for elem in list_of_dict
                if value in elem.values()]

# Value to be checked
value = 'Sydney'

# check if bool list contains any True element i.e.
# if any dictionary contains the given value or not
if any(list_of_bool):
    print(f"Yes, Value: '{value}' exists in list of dictionaries")
else:
    print(f"No, Value: '{value}' does not exists in list of dictionaries")

Output:

Yes, Value: 'Sydney' exists in list of dictionaries

Conclusion:

Here we discussed three different ways to check if a value exists in the list of dictionaries or not.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top