Open In App

Python – Test Record existence in Dictionary

Sometimes while working with a pool of records, we can have problems in which we need to check the presence of a particular value of a key for existence. This can have applications in many domains such as day-day programming or web development. Let us discuss certain ways in which this task can be performed.

Method #1 : Using any() + generator expression 



The combination of the above functions can be used to perform this task. In this, we simply test for all elements using any(), iterated using generator expression.




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
# Using any() + generator expression
 
# initializing list
test_list = [{'name': 'Nikhil', 'age': 22},
             {'name': 'Akshat', 'age': 23},
             {'name': 'Akash', 'age': 23}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
# Using any() + generator expression
res = any(sub[test_key] == test_val for sub in test_list)
 
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output : 
The original list is : [{‘name’: ‘Nikhil’, ‘age’: 22}, {‘name’: ‘Akshat’, ‘age’: 23}, {‘name’: ‘Akash’, ‘age’: 23}] 
Does key value contain in dictionary list : True 
 

Time Complexity: O(n) where n is the total number of values in the list “test_list”. 
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.



Method #2 : Using filter() + lambda 

The combination of the above functions can be used to perform this task. In this, we check for all values using filter and iteration using lambda function.
 




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
# Using filter() + lambda
 
# initializing list
test_list = [{'name': 'Nikhil', 'age': 22},
             {'name': 'Akshat', 'age': 23},
             {'name': 'Akash', 'age': 23}]
 
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
# Using filter() + lambda
res = filter(lambda sub: test_val in sub.values(), test_list)
if len(list(res)):
    res = True
else:
    res = False
 
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output : 
The original list is : [{‘name’: ‘Nikhil’, ‘age’: 22}, {‘name’: ‘Akshat’, ‘age’: 23}, {‘name’: ‘Akash’, ‘age’: 23}] 
Does key value contain in dictionary list : True 
 

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the filter() + lambda which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method #3 : Using keys() method




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
 
# initializing list
test_list = [{ 'name' : 'Nikhil', 'age' : 22},
            { 'name' : 'Akshat', 'age' : 23},
            { 'name' : 'Akash', 'age' : 23}]
 
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
res=False
for i in test_list:
    if test_key in i.keys() and i[test_key]==test_val:
        res=True
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : True

Time complexity: The time complexity of the given code is O(n), where n is the number of dictionaries in the test_list.

Auxiliary space: The auxiliary space used by the code is also O(1), which is a constant amount of space.

Method 4: Using a list comprehension

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
 
# initializing list
test_list = [{ 'name' : 'Nikhil', 'age' : 22},
            { 'name' : 'Akshat', 'age' : 23},
            { 'name' : 'Akash', 'age' : 23}]
 
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
res = any(i.get(test_key) == test_val for i in test_list)
 
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : True

Time complexity: O(n), where n is the length of the test_list since we need to iterate over each dictionary in the list once.
Auxiliary space: O(1), since we are only using a constant amount of extra memory to store the key-value pair to be searched for, the result variable, and the True/False list generated in the list comprehension.

Method #5: Using a for loop

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
 
# initializing list
test_list = [{ 'name' : 'Nikhil', 'age' : 22},
            { 'name' : 'Akshat', 'age' : 23},
            { 'name' : 'Akash', 'age' : 23}]
 
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
res = False
for d in test_list:
    if test_key in d and d[test_key] == test_val:
        res = True
        break
 
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : True

Time Complexity: O(n), where n is the length of test_list.
Auxiliary Space: O(1), as constant extra space is used.

Method #6: Using reduce():

Algorithm:




from functools import reduce
 
# initializing list
test_list = [{'name': 'Nikhil', 'age': 22},
             {'name': 'Akshat', 'age': 23},
             {'name': 'Akash', 'age': 23}]
 
# printing original list
print("The original list is:", test_list)
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary using reduce()
res = reduce(lambda x, y: x or (test_key in y and y[test_key] == test_val), test_list, False)
 
# printing result
print("Does key value contain in dictionary list:", res)
#This code is contributed by Pushpa.

Output
The original list is: [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list: True

Time complexity: O(n), where n is the length of the list of dictionaries. In the worst case, we may have to check all the dictionaries in the list.
Auxiliary Space: O(1), as we are not using any additional data structures that depend on the size of the input.

Method #7: Using dictionary comprehension

Step-by-step approach:




# Python3 code to demonstrate working of
# Test Record existence in Dictionary
 
# initializing list
test_list = [{ 'name' : 'Nikhil', 'age' : 22},
            { 'name' : 'Akshat', 'age' : 23},
            { 'name' : 'Akash', 'age' : 23}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing key and value
test_key = 'name'
test_val = 'Nikhil'
 
# Test Record existence in Dictionary
res = any(d[test_key] == test_val for d in test_list)
 
# printing result
print("Does key value contain in dictionary list : " + str(res))

Output
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : True

Time complexity: O(n)
Auxiliary space: O(1)


Article Tags :