Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a value exists in a dictionary (python)

I have the following dictionary in python:

d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} 

I need a way to find if a value such as "one" or "two" exists in this dictionary.

For example, if I wanted to know if the index "1" existed I would simply have to type:

"1" in d 

And then python would tell me if that is true or false, however I need to do that same exact thing except to find if a value exists.

like image 545
JimmyK Avatar asked Nov 21 '11 16:11

JimmyK


People also ask

How do you check if something exists in a dictionary?

FAQs on Finding If Key Exists in Dictionary You can check if a key exists or not in a dictionary using if-in statement/in operator, get(), keys(), handling 'KeyError' exception, and in versions older than Python 3, using has_key(). 2.

How do you check if a word is in a dictionary Python?

To simply check if a key exists in a Python dictionary you can use the in operator to search through the dictionary keys like this: pets = {'cats': 1, 'dogs': 2, 'fish': 3} if 'dogs' in pets: print('Dogs found!') # Dogs found! A dictionary can be a convenient data structure for counting the occurrence of items.

How do I find a value in a dictionary?

By using the dict. get() function, we can easily get the value by given key from the dictionary. This method will check the condition if the key is not found then it will return none value and if it is given then it specified the value.


2 Answers

>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} >>> 'one' in d.values() True 

Out of curiosity, some comparative timing:

>>> T(lambda : 'one' in d.itervalues()).repeat() [0.28107285499572754, 0.29107213020324707, 0.27941107749938965] >>> T(lambda : 'one' in d.values()).repeat() [0.38303399085998535, 0.37257885932922363, 0.37096405029296875] >>> T(lambda : 'one' in d.viewvalues()).repeat() [0.32004380226135254, 0.31716084480285645, 0.3171098232269287] 

EDIT: And in case you wonder why... the reason is that each of the above returns a different type of object, which may or may not be well suited for lookup operations:

>>> type(d.viewvalues()) <type 'dict_values'> >>> type(d.values()) <type 'list'> >>> type(d.itervalues()) <type 'dictionary-valueiterator'> 

EDIT2: As per request in comments...

>>> T(lambda : 'four' in d.itervalues()).repeat() [0.41178202629089355, 0.3959040641784668, 0.3970959186553955] >>> T(lambda : 'four' in d.values()).repeat() [0.4631338119506836, 0.43541407585144043, 0.4359898567199707] >>> T(lambda : 'four' in d.viewvalues()).repeat() [0.43414998054504395, 0.4213531017303467, 0.41684913635253906] 
like image 146
mac Avatar answered Oct 07 '22 21:10

mac


In Python 3, you can use

"one" in d.values() 

to test if "one" is among the values of your dictionary.

In Python 2, it's more efficient to use

"one" in d.itervalues() 

instead.

Note that this triggers a linear scan through the values of the dictionary, short-circuiting as soon as it is found, so this is a lot less efficient than checking whether a key is present.

like image 45
Sven Marnach Avatar answered Oct 07 '22 20:10

Sven Marnach