L3-2 Dictionary Helpful Methods

Here are some common functions that will come in handy when we need to update or access content in a dictionary

Function Description
[]
Returns the value of the
key
. If the
key
does not exist then throws
KeyError
.
get(<key>)
Returns the value of the
key
. If the
key
does not exist then returns
None
.
items()
Returns a list containing a tuple for each key value pair.
keys()
Returns a list containing the dictionary's keys.
values()
Returns a list containing the dictionary's values.
pop(<key>)
Removes the item with the
key
and returns its value.
clear()
Removes all items from the dictionary.
in
Check if the given key exist in the dictionary

Check if Key Exists in Dictionary

Similar to lists, we can use the

in
keyword to check the existence of a key in a dictionary.