VOOZH about

URL: https://www.geeksforgeeks.org/python/python-test-record-existence-in-dictionary/

⇱ Python - Test Record existence in Dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Test Record existence in Dictionary

Last Updated : 15 May, 2023

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.


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.
 


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


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:

  • 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:


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:

  • 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:


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:

  • 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

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:

  • 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.

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)

Comment