Built-in Dictionary Functions in Python
The following table lists all the functions that can be used with the Dictionary type in Python 3.
Method | Description |
---|---|
dict.clear() | Removes all the key-value pairs from the dictionary. |
dict.copy() | Returns a shallow copy of the dictionary. |
dict.fromkeys() | Creates a new dictionary from the given iterable (string, list, set, tuple) as keys and with the specified value. |
dict.get() | Returns the value of the specified key. |
dict.items() | Returns a dictionary view object that provides a dynamic view of dictionary elements as a list of key-value pairs. This view object changes when the dictionary changes. |
dict.keys() | Returns a dictionary view object that contains the list of keys of the dictionary. |
dict.pop() | Removes the key and return its value. If a key does not exist in the dictionary, then returns the default value if specified, else throws a KeyError. |
dict.popitem() | Removes and return a tuple of (key, value) pair from the dictionary. Pairs are returned in Last In First Out (LIFO) order. |
dict.setdefault() | Returns the value of the specified key in the dictionary. If the key not found, then it adds the key with the specified defaultvalue. If the defaultvalue is not specified then it set None value. |
dict.update() | Updates the dictionary with the key-value pairs from another dictionary or another iterable such as tuple having key-value pairs. |
dict.values() | Returns the dictionary view object that provides a dynamic view of all the values in the dictionary. This view object changes when the dictionary changes. |