The values from the dictionary can be accessed by their keys using the []
brackets. For example, we can fetch the color of an apple from the fruits_and_colors
dictionary in the following manner:
We can also use the .get()
method to access a value.
The difference between .get()
and []
is that if the key is not present in the dictionary, []
will throw an error, whereas .get()
will just return None
.
We can also pass an optional second parameter to .get()
in cases where we want a different default value to be returned when a key is absent in the dictionary.
Change the default value from deepred
to orange
and check the output.