Sometimes, while working with Python dictionaries, we can have a problem in which we need to test if, throughout the dictionary, the kth index of all values in the dictionary is equal to N. This kind of problem can occur in the web development domain. Let’s discuss certain ways in which this problem can be solved.
Input : test_dict = {‘Gfg’ : [10, 4, 8, 17], ‘best’ : [90, 4]}
Output : True
Input : test_dict = {‘Gfg’ : [10, 7]}
Output : False
Method #1: Using loop + items()
The combination of the above functions can be used to solve this problem. In this, we check for all items of the dictionary using items(), and a loop is used to iterate for dictionary keys and test for equality.
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = True
for key, val in test_dict.items():
if val[K - 1 ] ! = N:
res = False
print ( "Are all Kth index equal to N : " + str (res))
|
Output :
The original dictionary : {'is': [8, 4, 2], 'best': [7, 4, 9], 'Gfg': [1, 4, 8]}
Are all Kth index equal to N : True
Time complexity: O(n*k) where n is the number of key-value pairs in the dictionary and k is the length of the value list.
Auxiliary space: O(1) as the space used is constant and does not depend on the size of the input.
Method #2: Using all() + generator expression
The combination of the above functions can also be used to solve this problem. In this, we use all() to test for equality of all elements to N.
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = all ([val[K - 1 ] = = N for idx, val in test_dict.items()])
print ( "Are all Kth index equal to N : " + str (res))
|
Output :
The original dictionary : {'is': [8, 4, 2], 'best': [7, 4, 9], 'Gfg': [1, 4, 8]}
Are all Kth index equal to N : True
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1). This is because we are not using any extra data structures that grow with the size of the input.
Method #3 : Using for loop + len() method
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = False
x = [N] * len (test_dict)
y = []
for i in list (test_dict.keys()):
y.append(test_dict[i][K - 1 ])
if (x = = y):
res = True
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.
Method #4: Using list comprehension + all()
Uses a list comprehension to generate a list of boolean values indicating whether the Kth index of each value in the dictionary is equal to N or not. Then, all() function is used to check if all the boolean values in the list are True or not.
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = all (N = = lst[K - 1 ] for lst in test_dict.values())
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
Time Complexity: O(n), where n is the number of values in the dictionary. This is because we need to iterate over all the values in the dictionary to generate the list of boolean values.
Auxiliary Space: O(1). We are only using a constant amount of extra space to store the boolean values generated by the list comprehension.
Method #5: Using map() and all() method
This method uses the map() function to apply a lambda function that checks if the Kth index in each value list of the dictionary is equal to N or not. The resulting values are converted to a list and the all() function is used to check if all the values in the list are True or not.
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = all ( list ( map ( lambda x: x[K - 1 ] = = N, test_dict.values())))
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
The time complexity of the given code is O(N), where N is the number of values in the input dictionary.
The auxiliary space complexity of the given code is also O(N), as the code creates a list to store the resulting values from the map() function, which takes O(N) space.
Method 6: Using filter() and len()
This method uses the built-in function filter() along with the len() function to filter out the values that do not satisfy the condition, and then checks if the length of the filtered values is equal to the length of the original dictionary.
Approach:
- Initialize the dictionary test_dict.
- Initialize the variables K and N.
- Use the filter() function to filter out the values that do not satisfy the condition x[K-1] == N.
- Use the len() function to get the length of the filtered values.
- Check if the length of the filtered values is equal to the length of the original dictionary
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = len ( list ( filter ( lambda x: x[K - 1 ] = = N, test_dict.values()))) = = len (test_dict)
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(k), where k is the number of values that satisfy the condition x[K-1] == N.
Method #7: Using a list comprehension with any() function
Step-by-step approach:
- Initialize a boolean variable res to True
- Loop through each value list in the dictionary using a list comprehension
- Check if the Kth index of the value list is equal to N using a conditional statement
- If the condition is True for any value list, set res to True and break out of the loop
- If the condition is False for all value lists, set res to False
- Print the final value of res.
Below is the implementation of the above approach:
Python3
test_dict = { 'Gfg' : [ 1 , 4 , 8 ], 'is' : [ 8 , 4 , 2 ], 'best' : [ 7 , 4 , 9 ]}
print ( "The original dictionary : " + str (test_dict))
K = 2
N = 4
res = any (val[K - 1 ] = = N for val in test_dict.values())
print ( "Are all Kth index equal to N : " + str (res))
|
Output
The original dictionary : {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
Are all Kth index equal to N : True
Time Complexity: O(n), where n is the number of values in the dictionary
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Apr, 2023
Like Article
Save Article