Checking whether a Key or Value Exists in a Dictionary.

Mr #1738 - Aug 16 '23 - - Dev Community

In this article you will learn how to check whether a key or Value Exists in a Dictionay in python by use of in or not Operators.

Enter the following code into the interactive shell and see the output:

>>> spam = {'name':'vincent','age':24}
>>> 'name' in spam.keys()
True
>>> 24 in spam.values()
True
>>> 'age' in spam.keys()
True
>>> 'color' in spam
False
>>> 
Enter fullscreen mode Exit fullscreen mode

Note: if a Value or key doesn't exist in a dictionay it returns false.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .