Dictionary Methods in Python
Dictionary Methods in Python
Dictionary methods in Python are used to manipulate dictionary objects and retrieve information related to them. A dictionary object is a collection of key-value pairs and are stored within curly braces and separated by commas where key and value of every pair is separated by a colon. A number of methods of dictionary class and functions that act on them are available; some of which are listed below in the form of a table.
Methods and Functions | Description |
clear() | It is a method and is used to delete all the elements of the given dictionary object and this makes it empty. |
copy() | It is a method and is used to create a copy of the given dictionary object. |
fromkeys() | It is a method and is used to create a dictionary object with the specified keys and values. |
get() | It is a method and is used to return the value of the specified key of the given dictionary object. |
items() | It is a method and returns a list of tuples where each tuple has a key-value pair of the given dictionary object. |
keys() | It is a method and is used to return a list of all the keys that given dictionary object has. |
pop() | It is a method and is used to remove the specified key-value pair from the given dictionary object. |
popitem() | It is a method and is used to remove the last key-value pair from the given dictionary object. |
setdefault() | It is a method and it checks for the presence of the specified key in the given dictionary. If it does not exist, then it inserts the specified key-value pair in the object. |
update() | It is a method and updates the dictionary with the specified key-value pairs. |
values() | It is a method and is used to return a list of all the values of the given dictionary object. |
len() | It is a function and is used to return the number of key-value pairs the given dictionary object has. |
dict() | It is a function and is used to create a dictionary object from the specified iterable object containing key-value pairs. |