This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to remove an element from a dictionary in Python.
Let’s say you have a dictionary of users.
user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}
Remove Element From Dictionary in Python
In order to remove an element from a dictionary, you can use the pop()
method.
user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}
# Name of key for the element removal
elementToRemove = user.pop("Bryan")
# Display removed element
print('The removed element is:', elementToRemove)
# The removed element is: 18
# Display dictionary after removing element
print('Current dictionary:', user)
# Current dictionary: {'Adam': 20, 'Cleo': 23}
Note: The pop()
method functions by removing an element from a dictionary based on the supplied key.
The post How to Remove Element From Dictionary in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-27T10:11:35+00:00) How to Remove Element From Dictionary in Python. Retrieved from https://www.scien.cx/2021/02/27/how-to-remove-element-from-dictionary-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.