In this section, we will learn what methods a dictionary object provides in Python and explain the job of each one of them.
Python Dictionary Methods List:
There are multiple useful methods that are provided for the dictionary object in Python. Using these methods, we can remove, update the keys in a dictionary or get a different type of information about a dictionary in Python.
Here’s the list of dictionary methods:
Method | Description |
clear() | The Python Dictionary clear() method is used to clear the entire elements of a Dictionary. |
copy() | The Python Dictionary copy() method is used to get a copy of the target dictionary. |
fromkeys() | The Python Dictionary fromkeys() method is used to create a dictionary from the items of an iterable object. |
get() | The Python Dictionary get() method is used to get the value of a key in a dictionary. |
items() | The Python Dictionary items() method is used to get a view of the target dictionary.
This view is basically a list with each pair of key/value of the target Dictionary set in a tuple within this list. |
keys() | The Python Dictionary keys() method is used to get an object view of the target Dictionary that contains all its keys set as the items of a list. |
pop() | The Python Dictionary pop() method is used to remove the specified key (set as the first argument of this method) from the target Dictionary. |
popitem() | The Python dictionary popitem() method is used to remove the last entry (the last pair of key, value) that was inserted into the target Dictionary object. |
setdefault() | The Python Dictionary setdefault() method is used for two cases:
|
update() | The Python Dictionary update() method is used to add new items to a dictionary. |
values() | The Python Dictionary values() method is used to get an object view of the target Dictionary that contains all its values set as the items of a list. |
In the incoming sections we’ve covered these methods in greater details.