Python dictionary methods
Python Dictionary Methods
A dictionary in Python is an object of the built-in dict class, which defines the following methods –
Sequence Number | Method and Description |
---|---|
1 | dict.clear() Clears all elements from the dictionary dict. |
2 | dict.copy() Returns a shallow copy of the dictionary dict. |
3 | dict.fromkeys() Creates a new dictionary using the elements in seq as keys and setting the values to value. |
4 | dict.get(key, default=None) For a given key, returns value or default if the key is not in the dictionary. |
5 | dict.has_key(key) Returns true if the given key is available in the dictionary, false otherwise. |
6 | dict.items() Returns all key-value pairs in a dictionary. |
7 | dict.keys() Returns all keys in a dictionary. |
8 | dict.pop() Deletes the element with the specified key from the set. |
9 | dict.popitem() Deletes the last inserted key-value pair. |
10 | dict.setdefault(key, default=None) Similar to the get() method, if the key key is not in the dictionary, sets dict[key] to default. |
11 | dict.update(dict2) Adds the key-value pair of dictionary (dict2) to dictionary (dict). |
12 | dict.values() Returns all values in a dictionary (dict). |