In this article, we will see how to find all the keys associated with a single value or multiple values.
For instance, we have a dictionary of words.
dictOfWords = {"hello": 56, "at" : 23 , "test" : 43, "this" : 97, "here" : 43, "now" : 97 }
Now, we want all the keys in the dictionary having value 43, which means “here” and “test”.
Let’s see how to get it.
Find keys by value in the dictionary
Dict.items() is the module that returns all the key-value pairs in a dictionary. So, what we will do is check whether the condition is satisfied by iterating over the sequence. If the value is the same then we will add the key in a separate list.
def getKeysByValue(dictOfElements, valueToFind): listOfKeys = list() listOfItems = dictOfElements.items() for item in listOfitems: if item[1] == valueToFind: listOfKeys.append(item[0]) return listOfKeys
listOfKeys = getKeysByValue(dictOfWords, 43) print("Keys with value equal to 43") #Iterate over the list of keys for key in listOfKeys: print(key)
We can achieve the same thing with a list comprehension.
listOfKeys = [key for (key, value) in dictOfWords.items() if value == 43]
Find keys in the dictionary by value list
Now, we want to find the keys in the dictionary whose values matches with the value we will give.
[43, 97]
We will do the same thing as we have done above but this time we will iterate the sequence and check whether the value matches with the given value.
def getKeysByValues(dictOfElements, listOfValues): listOfKeys = list() listOfItems = dictOfElements.items() for item in listOfItems: if item[1] in listOfValues: listOfKeys.append(item[0]) return listOfKeys
We will use the above function:
listOfKeys = getKeysByValues(dictOfWords, [43, 97] ) #Iterate over the list of values for key in listOfKeys: print(key)
Complete Code:
'''Get a list of keys from dictionary which has the given value''' def getKeysByValue(dictOfElements, valueToFind): listOfKeys = list() listOfItems = dictOfElements.items() for item in listOfItems: if item[1] == valueToFind: listOfKeys.append(item[0]) return listOfKeys ''' Get a list of keys from dictionary which has value that matches with any value in given list of values ''' def getKeysByValues(dictOfElements, listOfValues): listOfKeys = list() listOfItems = dictOfElements.items() for item in listOfItems: if item[1] in listOfValues: listOfKeys.append(item[0]) return listOfKeys def main(): # Dictionary of strings and int dictOfWords = { "hello": 56, "at" : 23 , "test" : 43, "this" : 97, "here" : 43, "now" : 97} print("Original Dictionary") print(dictOfWords) ''' Get list of keys with value 43 ''' listOfKeys = getKeysByValue(dictOfWords, 43) print("Keys with value equal to 43") #Iterate over the list of keys for key in listOfKeys: print(key) print("Keys with value equal to 43") ''' Get list of keys with value 43 using list comprehension ''' listOfKeys = [key for (key, value) in dictOfWords.items() if value == 43] #Iterate over the list of keys for key in listOfKeys: print(key) print("Keys with value equal to any one from the list [43, 97] ") ''' Get list of keys with any of the given values ''' listOfKeys = getKeysByValues(dictOfWords, [43, 97] ) #Iterate over the list of values for key in listOfKeys: print(key) if __name__ == '__main__': main()
Hope this article was useful for you.
Enjoy Reading!