Python Dictionary Methods
Python dictionaries have several built-in methods for manipulating key-value pairs. Here is a table of all the methods of the dictionary:
Methods | Description |
get() | Returns the value for the key safely. Avoids KeyError . |
keys() | Returns a view object with all dictionary keys. |
values() | Returns a view object with all dictionary values. |
items() | Returns a view object with all key-value pairs as tuples. |
update() | Updates the dictionary with key-value pairs from another dictionary, overwriting existing keys. |
pop() | Removes and returns the value of the specified key. |
clear() | Removes all items from the dictionary. |
copy() | Returns a shallow copy of the dictionary. |
setdefault() | Returns value of key if present, else inserts key with default value and returns default. |
fromkeys() | Create a new dictionary with keys from an iterable, all assigned the same specified value (or None if no value is specified). |
popitem() | Removes and returns the last inserted key-value pair as a tuple (LIFO order in Python 3.7+). |