Skip to content
Related Articles

Related Articles

Python – Test Kth index in Dictionary value list
  • Last Updated : 10 May, 2020

Sometimes, while working with Python dictionaries, we can have a problem in which we need to test if througout the dictionary, the kth index of all values in dictionary is equal to N. This kind of problem can occur in 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 above functions can be used to solve this problem. In this, we check for all items of dictionary using items() and loop is used to iterate for dictionary keys and testing for equality.




# 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

 

Method #2 : Using all() + generator expression
The combination of 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 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :