This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to remove a key from a dictionary in Python.
Let’s say you have a dictionary of users.
user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}
Remove Key From Dictionary in Python
In order to remove a key from a dictionary, you can use the del
keyword.
user = {"Adam" : 20, "Bryan" : 18, "Cleo" : 23}
# Delete key-value pair for "Bryan"
del user["Bryan"]
print(user)
# {'Adam': 20, 'Cleo': 23}
Note: The del
keyword functions by deleting Python’s objects.
The post How to Remove Key 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-27T09:42:55+00:00) How to Remove Key From Dictionary in Python. Retrieved from https://www.scien.cx/2021/02/27/how-to-remove-key-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.