![]() |
VOOZH | about |
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.
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.
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
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : TrueTime 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:
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : TrueTime 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:
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : TrueTime 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:
The original list is: [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list: TrueTime 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:
The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : TrueTime complexity: O(n)
Auxiliary space: O(1)