Open In App

Python – Test Kth index in Dictionary value list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using items() + loop
 
# initializing dictionary
test_dict = {'Gfg' : [1, 4, 8], 'is' : [8, 4, 2], 'best' : [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using items() + loop
res = True
for key, val in test_dict.items():
    if val[K - 1] != N:
        res = False
 
# printing result
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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using all() + generator expression
 
# initializing dictionary
test_dict = {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using all() + generator expression
res = all([val[K - 1] == N for idx, val in test_dict.items()])
 
# printing result
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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
 
# initializing dictionary
test_dict = {'Gfg' : [1, 4, 8], 'is' : [8, 4, 2], 'best' : [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
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
     
 
# printing result
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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using list comprehension + all()
 
# initializing dictionary
test_dict = {'Gfg': [1, 4, 8], 'is': [8, 4, 2], 'best': [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using list comprehension + all()
res = all(N == lst[K-1] for lst in test_dict.values())
 
# printing result
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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using map() + all()
 
# initializing dictionary
test_dict = {'Gfg' : [1, 4, 8], 'is' : [8, 4, 2], 'best' : [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using map() + all()
res = all(list(map(lambda x: x[K-1] == N, test_dict.values())))
 
# printing result
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:

  1. Initialize the dictionary test_dict.
  2. Initialize the variables K and N.
  3. Use the filter() function to filter out the values that do not satisfy the condition x[K-1] == N.
  4. Use the len() function to get the length of the filtered values.
  5. Check if the length of the filtered values is equal to the length of the original dictionary

Python3




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using filter() + len()
 
# initializing dictionary
test_dict = {'Gfg' : [1, 4, 8], 'is' : [8, 4, 2], 'best' : [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using filter() + len()
res = len(list(filter(lambda x: x[K-1] == N, test_dict.values()))) == len(test_dict)
 
# printing result
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




# Python3 code to demonstrate working of
# Test Kth index in Dictionary value list
# Using list comprehension with any()
 
# initializing dictionary
test_dict = {'Gfg' : [1, 4, 8], 'is' : [8, 4, 2], 'best' : [7, 4, 9]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 2
 
# initializing N
N = 4
 
# Test Kth index in Dictionary value list
# Using list comprehension with any()
res = any(val[K-1] == N for val in test_dict.values())
 
# printing result
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)



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads