Python Dictionary Methods
Last Updated :
11 May, 2024
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
|
---|
clear()
| Removes all items from the dictionary
|
---|
copy()
| Returns a shallow copy of the dictionary
|
---|
fromkeys()
| Creates a dictionary from the given sequence
|
---|
get()
| Returns the value for the given key
|
---|
items()
| Return the list with all dictionary keys with values
|
---|
keys()
| Returns a view object that displays a list of all the keys in the dictionary in order of insertion
|
---|
pop()
| Returns and removes the element with the given key
|
---|
popitem()
| Returns and removes the key-value pair from the dictionary
|
---|
setdefault()
| Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary
|
---|
values()
| Returns a view object containing all dictionary values, which can be accessed and iterated through efficiently
|
---|
update()
| 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.
Python
my_dict = {'1': 'Geeks', '2': 'For', '3': 'Geeks'}
my_dict.clear()
print(my_dict)
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.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(d.get('Name'))
print(d.get('Gender'))
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.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.items())[1][0])
print(list(d.items())[1][1])
4. Dictionary keys() Method
The keys() method in Python returns a view object with dictionary keys, allowing efficient access and iteration.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.keys()))
Output['Name', 'Age', 'Country']
5. Dictionary values() Method
The values() method in Python returns a view object containing all dictionary values, which can be accessed and iterated through efficiently.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
print(list(d.values()))
Output['Ram', '19', 'India']
6. 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.
Python
d1 = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d2 = {'Name': 'Neha', 'Age': '22'}
d1.update(d2)
print(d1)
Output{'Name': 'Neha', 'Age': '22', 'Country': '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.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d.pop('Age')
print(d)
Output{'Name': 'Ram', 'Country': 'India'}
8. Dictionary popitem() Method
In Python, the popitem() method is a dictionary function that eliminates and returns a random (key, value) pair from the dictionary.
As opposed to the pop() method which gets rid of a particular key-value pair based on a given key, popitem() takes out and gives back a pair without requiring a key to be specified.
Python
d = {'Name': 'Ram', 'Age': '19', 'Country': 'India'}
d.popitem()
print(d)
d.popitem()
print(d)
Output{'Name': 'Ram', 'Age': '19'}
{'Name': 'Ram'}
FAQs on Python Dictionary Methods
Q1: What is a Python Dictionary?
In Python, a dictionary is like a container that holds an assortment of key-value pairs, It’s a fundamental way to organize data where each piece of information is known as a “Key”. The Dictionary doesn’t impose any specific order on these pairs, so you can’t rely on the sequence in which they were added.
Q2: How we can access values in a Python dictionary?
We can access values in a Python dictionary by using the keys as the index: Below is the code:
my_dict = {“name”: “Kin”, “age”: 23, “city”: “London”}
print(my_dict[“name”]) #kin
print(my_dict[“age”]) #23
print(my_dict[“city”]) #London
Q3: What happens when we try to access a key that doesn’t exist in the dictionary?
When we try to access a key that doesn’t exist in the dictionary, Python will raise a ‘KeyError’. You can use the ‘get()’ method.
Q4: How do we remove an item from a dictionary?
To remove an item (key-value pair) from a dictionary, you can use the ‘pop()’ method, specifying the key to be removed.
Please Login to comment...