Check if a Value Already Exists in a List of Dictionary Objects in Python
Sometimes, you may come across scenarios where you need to check whether a specific value exists within a list of dictionary objects. In this tutorial, we will show you how to determine if a value is present in a list of dictionary objects in Python.
Here is an example code in which we want to check if a specific value, such as "sam@tutorialsbuddy.com", exists in the "email" key of any of these dictionaries:
customer =[
{
"firstname": "Danny",
"email": "dan@tutorialsbuddy.com"
},
{
"firstname": "Sam",
"email": "sam@tutorialsbuddy.com"
},
{
"firstname": "Peter",
"email": "pet@tutorialsbuddy.com"
}
]
result = any(c['email'] == 'sam@tutorialsbuddy.com' for c in customer)
if result:
print("Email sam@tutorialsbuddy.com exists!")
else:
print('Email sam@tutorialsbuddy.com does not exist!')
The output of the above code is as follows:
Email sam@tutorialsbuddy.com exists!