{}
or the dict()
constructor.KeyError
. Use the get()
method to avoid this.=
.pop()
to remove a key-value pair by key (returns the value).popitem()
to remove the last inserted key-value pair (Python 3.7+).del
to delete a key-value pair.clear()
to remove all key-value pairs.in
keyword to check if a key exists in the dictionary.len()
to find the number of key-value pairs in a dictionary.keys()
values()
items()
update()
copy()
{key: value for item in iterable}
get()
to avoid KeyError
when accessing keys that may not exist.