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
# 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)) |
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
# 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)) |
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
# 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)) |
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:
- Initialize the list test_list.
- Print the original list using the print() function.
- Initialize the key-value pair to be searched for.
- Use a list comprehension to generate a list of True and False values based on whether the key-value pair is present in each dictionary in test_list.
- Check if any of the values in the list generated in step 4 are True.
- Print the final result.
Below is the implementation of the above approach:
Python3
# 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)) |
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:
- Two variables test_key and test_val are initialized with the key and value to be searched in the dictionaries.
- A boolean variable res is initialized with False.
- A for loop is used to iterate through each dictionary d in test_list.
- Inside the for loop, an if condition is used to check if the current dictionary d contains the test_key and its value is equal to test_val.
- If the above condition is satisfied, the res variable is set to True and the loop is broken using the break statement.
- The value of res is printed using the print() function.
Below is the implementation of the above approach:
Python3
# 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)) |
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:
- Import the reduce function from functools module.
- Initialize the list of dictionaries.
- Initialize the key and value to search in the list of dictionaries.
- Use the reduce function to iterate over the list of dictionaries and check if the key exists and the value matches the provided value using the lambda function.
- If any dictionary in the list satisfies the condition, return True, else False.
- Print the result
Python3
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. |
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:
- Initialize the list of dictionaries called “test_list”.
- Print the original list using the “print()” function.
- Initialize the key and value that need to be checked for existence in the dictionaries of the list.
- Use the “any()” function with a generator expression to check if the key-value pair exists in any of the dictionaries in the list.
- Store the result in a variable called “res”.
- Print the result using the “print()” function.
Python3
# 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)) |
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)