Python dictionary example to find keys with the same value

Introduction :

A python dictionary is a collection of key-value pairs. All keys in a python dictionary should be different.No two keys can have the same name. But for different keys, we can have the same ‘value’. This tutorial is to show you how we can find all keys in a python dictionary with the same value.
We will create one program that will :
1. Create one dictionary by taking the inputs from the user.
2. The user will enter one value to find in the dictionary.
3. It will find if more than two keys have the same value entered by the user.
4. The program will also print out the keys for that value if found.

Solve it by using a loop :

This is the simplest option to look for anything in a dictionary.
At first, the program will get the full list of key-value pairs from the dictionary and then using one for loop, it will compare each pair one by one.
We will take all inputs from the user, i.e. the user will enter the key-value pairs for the dictionary and also the value.

Python program :

Before going into details, let me show you the python program (The source code is available here): 

python dictionary find keys with same value

Explanation :

The commented numbers in the above program denote the step numbers below :

1. given_dict is an empty dictionary. We will take the inputs from the user and insert them to this dictionary.result_set is an empty set for storing the final keys.
2. Ask the user to enter the total number of key-value pairs for the dictionary. Read and store it in total_values variable.
3. Run one for loop for total_values time.
4. Ask the user to enter key and value for the current index separated by a comma and store it in key_value_str variable. For example, if the user will enter one,1, we will store ‘one,1’ string in key_value_str.
5. Split the variable key_value_str by . Store the result in key_value variable. If the string was “one,1”, after splitting, key_value will hold “one” in its 0th index and “1” in its 1st index position.
6. Add the key-value pairs to the dictionary. Remember that the key is stored in the 0th index and the value is stored in the first index.
7. Ask the user to enter the value to find in the dictionary.Store it in value_to_find variable.
8. Using items() function, get the list of all key-value pairs of the dictionary. Store the list in value_to_find variable.
9. Now, iterate all items in the list one by one using a ‘for loop’.
10. For each item in the list, check if any value is equal to the user input value value_to_find. If yes, add the key for that value to the final result set.
11. Finally, using one for loop, print out the list of keys for the user input value.

python dictionary find key with same value

Sample Output :

python dictionary find keys with same value

Conclusion :

As you have seen that using one loop, we can easily find out all keys for a specific value in python. You can also modify the above program to check for multiple values instead of just one. Try to run the above program using python 3 and drop one comment below if you have any queries.

 

You might also like :

Leave a Reply