{"id":3488,"date":"2021-04-27T19:37:19","date_gmt":"2021-04-27T14:07:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=3488"},"modified":"2021-11-22T18:43:03","modified_gmt":"2021-11-22T13:13:03","slug":"python-how-to-find-keys-by-value-in-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-find-keys-by-value-in-dictionary\/","title":{"rendered":"Python : How to find keys by value in dictionary ?"},"content":{"rendered":"
In this article, we will see how to find all the keys associated with a single value or multiple values.<\/p>\n
For instance, we have a dictionary of words.<\/p>\n
dictOfWords = <\/span>{<\/span>\"hello\": <\/span>56<\/span>,\r\n<\/span>\"at\" : <\/span>23<\/span> ,\r\n<\/span>\"test\" : <\/span>43<\/span>,\r\n<\/span>\"this\" : <\/span>97<\/span>,\r\n<\/span>\"here\" : <\/span>43<\/span>,\r\n<\/span>\"now\" : <\/span>97\r\n<\/span>}<\/pre>\n<\/div>\n\nNow, we want all the keys in the dictionary having value 43, which means “here” and “test”.<\/p>\n
Let’s see how to get it.<\/p>\n
Find keys by value in the dictionary<\/h2>\n
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.<\/p>\n
\ndef<\/span> getKeysByValue<\/span>(<\/span>dictOfElements, valueToFind<\/span>)<\/span>:\r\n<\/span> listOfKeys = list<\/span>()\r\n<\/span>listOfItems = dictOfElements.items<\/span>()\r\n<\/span>for item <\/span>in<\/span> listOfitems:\r\n<\/span>if item<\/span>[<\/span>1<\/span>]<\/span> == valueToFind:\r\n<\/span> listOfKeys.append<\/span>(<\/span>item<\/span>[<\/span>0<\/span>])\r\n<\/span>return listOfKeys<\/span><\/pre>\n<\/div>\nWe will use this function to get the keys by value 43.<\/div>\n\n\nlistOfKeys = <\/span>getKeysByValue<\/span>(<\/span>dictOfWords, <\/span>43<\/span>)\r\n<\/span>print(<\/span>\"Keys with value equal to 43\"<\/span>)\r\n<\/span>#Iterate over the list of keys\r\nfor key <\/span>in<\/span> listOfKeys:\r\n<\/span>print(<\/span>key)<\/span><\/pre>\n<\/div>\n\n<\/p>\n
We can achieve the same thing with a list comprehension.<\/p>\n
listOfKeys = <\/span>[<\/span>key <\/span>for<\/span> (<\/span>key, value<\/span>)<\/span> in<\/span> dictOfWords.<\/span>items<\/span>()<\/span> if<\/span> value == <\/span>43<\/span>]<\/span><\/pre>\nFind keys in the dictionary by value list<\/h3>\n
Now, we want to find the keys in the dictionary whose values matches with the value we will give.<\/p>\n
[<\/span>43<\/span>, <\/span>97<\/span>]<\/span><\/pre>\nWe 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.<\/p>\n
\ndef<\/span> getKeysByValues<\/span>(<\/span>dictOfElements, listOfValues<\/span>)<\/span>:\r\n<\/span>listOfKeys = list<\/span>()\r\n<\/span>listOfItems = dictOfElements.items<\/span>()\r\n<\/span>for item <\/span>in<\/span> listOfItems:\r\n<\/span>if item<\/span>[<\/span>1<\/span>]<\/span> in<\/span> listOfValues:\r\n<\/span> listOfKeys.append<\/span>(<\/span>item<\/span>[<\/span>0<\/span>])\r\n<\/span>return listOfKeys<\/span><\/pre>\nWe will use the above function:<\/p>\n
\nlistOfKeys = <\/span>getKeysByValues<\/span>(<\/span>dictOfWords, <\/span>[<\/span>43<\/span>, <\/span>97<\/span>]<\/span> )\r\n<\/span>#Iterate over the list of values\r\nfor key <\/span>in<\/span> listOfKeys:\r\n<\/span>print(<\/span>key<\/span>)<\/span><\/pre>\n<\/p>\n<\/div>\n
Complete Code:<\/strong><\/em><\/p>\n\n'''Get a list of keys from dictionary which has the given value'''\r\ndef getKeysByValue(dictOfElements, valueToFind):\r\nlistOfKeys = list()\r\nlistOfItems = dictOfElements.items()\r\nfor item in listOfItems:\r\nif item[1] == valueToFind:\r\nlistOfKeys.append(item[0])\r\nreturn listOfKeys\r\n'''\r\nGet a list of keys from dictionary which has value that matches with any value in given list of values\r\n'''\r\ndef getKeysByValues(dictOfElements, listOfValues):\r\nlistOfKeys = list()\r\nlistOfItems = dictOfElements.items()\r\nfor item in listOfItems:\r\nif item[1] in listOfValues:\r\nlistOfKeys.append(item[0])\r\nreturn listOfKeys\r\ndef main():\r\n# Dictionary of strings and int\r\ndictOfWords = {\r\n\"hello\": 56,\r\n\"at\" : 23 ,\r\n\"test\" : 43,\r\n\"this\" : 97,\r\n\"here\" : 43,\r\n\"now\" : 97}\r\nprint(\"Original Dictionary\")\r\nprint(dictOfWords)\r\n'''\r\nGet list of keys with value 43\r\n'''\r\nlistOfKeys = getKeysByValue(dictOfWords, 43)\r\nprint(\"Keys with value equal to 43\")\r\n#Iterate over the list of keys\r\nfor key in listOfKeys:\r\nprint(key)\r\nprint(\"Keys with value equal to 43\")\r\n'''\r\nGet list of keys with value 43 using list comprehension\r\n'''\r\nlistOfKeys = [key for (key, value) in dictOfWords.items() if value == 43]\r\n#Iterate over the list of keys\r\nfor key in listOfKeys:\r\nprint(key)\r\nprint(\"Keys with value equal to any one from the list [43, 97] \")\r\n'''\r\nGet list of keys with any of the given values\r\n'''\r\nlistOfKeys = getKeysByValues(dictOfWords, [43, 97] )\r\n#Iterate over the list of values\r\nfor key in listOfKeys:\r\nprint(key)\r\nif __name__ == '__main__':\r\nmain()<\/pre>\n <\/p>\n<\/div>\n
\nHope this article was useful for you.<\/p>\n
Enjoy Reading!<\/strong><\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"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 …<\/p>\n