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 : TrueInput : 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 = Truefor 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)) |
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)) |
The original dictionary : {'is': [8, 4, 2], 'best': [7, 4, 9], 'Gfg': [1, 4, 8]}
Are all Kth index equal to N : True
Recommended Posts:
- Python - Extract Kth index elements from Dictionary Value list
- Python - How to Sort a Dictionary by Kth Index Value
- Python - Convert Dictionary Value list to Dictionary List
- Python - Sort Dictionary List by Key's ith Index value
- Python - Convert List to Index and Value dictionary
- Python - Replace String by Kth Dictionary value
- Python - Test for Empty Dictionary Value List
- Python - Every Kth index Maximum in List
- Python - Sort Records by Kth Index List
- Python - Kth Index Tuple List Mean
- Python - Sort from Kth index in List
- Python - Replace dictionary value from other dictionary
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert list to Single Dictionary Key Value list
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Assigning Key values to list elements from Value list Dictionary
- Python - Convert String List to Key-Value List dictionary
- Python | Dictionary with index as value
- Python - Sort dictionaries list by Key's Value list index
- Python | Initializing dictionary with list index values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

