In this article we will discuss how to find all keys associated with a given single value or multiple values.

Suppose we have a dictionary of words and thier frequency i.e.

```# Dictionary of strings and int
dictOfWords = {
"hello": 56,
"at" : 23 ,
"test" : 43,
"this" : 97,
"here" : 43,
"now" : 97
}
```

Now we want to get all the keys in dictionary whose value is 43. Like in our case there will two keys who has value 43 i.e.
```here
test```

Now let’s see how to get the list of keys by given value

## Find keys by value in dictionary

As, dict.items() returns an iterable sequence of all key value pairs in dictionary. So, we will iterate over this sequence and for each entry we will check if value is same as given value then we will add the key in a separate list i.e.

```'''
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 == valueToFind:
listOfKeys.append(item)
return  listOfKeys```

Now let’s use this function to get keys by value 43 i.e.
```'''
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)
```

Output:
```here
test```

Same can be achieved by List Comprehensions i.e.
```'''
Get list of keys with value 43 using list comprehension
'''
listOfKeys = [key  for (key, value) in dictOfWords.items() if value == 43]```

## Find keys in dictionary by value list

Suppose we want to find all the keys in dictionary whose value matches with any of the value given in list i.e.

`[43, 97]`

To do that we will iterate over iterable sequence returned by dict.items() and for each entry we will check if its value matches with any entry from the given value list, if yes then we will add that key in a separate list i.e.
```'''
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 in listOfValues:
listOfKeys.append(item)
return  listOfKeys
```

Now lets use this to find all the keys from dictionary whose values is equal to any value from the list i.e.
```'''
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)```

Output:
```Keys with value equal to any one from the list [43, 97]
this
here
now
test```

## Python Dictionary Tutorial - Series:

### Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.

Complete example is as follows,

```'''
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 == valueToFind:
listOfKeys.append(item)
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 in listOfValues:
listOfKeys.append(item)
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()

```

Output:
```Original Dictionary
{'hello': 56, 'at': 23, 'this': 97, 'here': 43, 'test': 43, 'now': 97}
Keys with value equal to 43
here
test
Keys with value equal to 43
here
test
Keys with value equal to any one from the list [43, 97]
this
here
test
now
```