Python Dictionary Methods
Python dictionary methods is collection of Python functions that operates on Dictionary.
Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the functions provided by Python to work with dictionaries.
List of Python Dictionary Methods
Python provides several built-in methods for dictionaries that allow for efficient manipulation, access, and transformation of dictionary data. Here's a list of some important Python dictionary methods:
Functions Name | Descriptions |
---|---|
Removes all items from the dictionary | |
Returns a shallow copy of the dictionary | |
Creates a dictionary from the given sequence | |
Returns the value for the given key | |
Return the list with all dictionary keys with values | |
Returns a view object that displays a list of all the keys in the dictionary in order of insertion | |
Returns and removes the element with the given key | |
Returns and removes the item that was last inserted into the dictionary. | |
Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary | |
Returns a view object containing all dictionary values, which can be accessed and iterated through efficiently | |
Updates the dictionary with the elements from another dictionary or an iterable of key-value pairs. With this method, you can include new data or merge it with existing dictionary entries |
These methods provide various functionalities for working with dictionaries in Python, making it easier to manage and manipulate data stored in key-value pairs.
Note: For more information on Python Dictionary refer to Python Dictionary Tutorial.
Built-in Dictionary Methods in Python
In Python Dictionary we have various built-in functions that provide a wide range of operations for working with dictionaries. These techniques enable efficient manipulation, access, and transformation of dictionary data.
Lets Look at some Python dictionary methods with examples:
1. Dictionary clear() Method
The clear() method in Python is a built-in method that is used to remove all the elements (key-value pairs) from a dictionary. It essentially empties the dictionary, leaving it with no key-value pairs.
my_dict = {'1': 'Geeks', '2': 'For', '3': 'Geeks'}
my_dict.clear()
print(my_dict)
Output
{}
2. Dictionary get() Method
In Python, the get() method is a pre-built dictionary function that enables you to obtain the value linked to a particular key in a dictionary. It is a secure method to access dictionary values without causing a KeyError if the key isn't present.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(d.get('Name'))
print(d.get('Gender'))
Output
Ram None
3. Dictionary items() Method
In Python, the items() method is a built-in dictionary function that retrieves a view object containing a list of tuples. Each tuple represents a key-value pair from the dictionary. This method is a convenient way to access both the keys and values of a dictionary simultaneously, and it is highly efficient.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.items())[1][0])
print(list(d.items())[1][1])
Output
Age 19
4. Dictionary keys() Method
The keys() method in Python returns a view object with dictionary keys, allowing efficient access and iteration.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.keys()))
Output
['Name', 'Age', 'Country']
5. Dictionary update() Method
Python's update() method is a built-in dictionary function that updates the key-value pairs of a dictionary using elements from another dictionary or an iterable of key-value pairs. With this method, you can include new data or merge it with existing dictionary entries.
d1 = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d2 = {'Name': 'Neha', 'Age': '22'}
d1.update(d2)
print(d1)
Output
{'Name': 'Neha', 'Age': '22', 'Country': 'India'}
6. Dictionary values() Method
The values() method in Python returns a view object containing all dictionary values, which can be accessed and iterated through efficiently.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.values()))
Output
['Ram', '19', 'India']
7. Dictionary pop() Method
In Python, the pop() method is a pre-existing dictionary method that removes and retrieves the value linked with a given key from a dictionary. If the key is not present in the dictionary, you can set an optional default value to be returned.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d.pop('Age')
print(d)
Output
{'Name': 'Ram', 'Country': 'India'}
8. Dictionary popitem() Method
The popitem() method in Python dictionaries is used to remove and return the last inserted key-value pair as a tuple. If the dictionary is empty then it raises a KeyError.
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
val = d.popitem()
print(val)
val = d.popitem()
print(val)
Output
('Country', 'India') ('Age', '19')