Skip to content
Advertisement

How to check if a value exists in a dictionary?

I have the following dictionary in python:

JavaScript

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:

JavaScript

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.

Advertisement

Answer

JavaScript

Out of curiosity, some comparative timing:

JavaScript

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:

JavaScript

EDIT2: As per request in comments…

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement