Computer Science
Can you check for a value inside a dictionary using in operator? How will you check for a value inside a dictionary using in operator ?
Python Dictionaries
8 Likes
Answer
We cannot directly check for a value inside a dictionary using in operator since in operator searches for a key in a dictionary. We can use values() function along with in operator to check for a value in dictionary as shown below:
>>> d = {1: 'a' , 2:'b'}
>>> 'b' in d.values()
Output
True
Answered By
4 Likes
Related Questions
Can you change the order of dictionary's contents, i.e., can you sort the contents of a dictionary ?
In no more than one sentence, explain the following Python error and how it could arise:
TypeError: unhashable type: 'list'
Dictionary is a mutable type, which means you can modify its contents ? What all is modifiable in a dictionary ? Can you modify the keys of a dictionary ?
How is
del D
anddel D[<key>]
different from one another if D is a dictionary ?