Example of Python dictionary
>>>> animals = {'lion': 'carnivore', 'giraffe': 'herbivore'}
>>> print('lion' in animals)
True
>>> if 'lion' in animals:
>>> print("In dict")
In dict
/ #Python
To prevent errors, we sometimes want to check if a key exists in a Python dictionary. Here's how you do that.
>>>> animals = {'lion': 'carnivore', 'giraffe': 'herbivore'}
>>> print('lion' in animals)
True
>>> if 'lion' in animals:
>>> print("In dict")
In dict
No comments yet...