How to Remove Key From Dictionary in Python
Posted on: February 27, 2021 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.
Share on social media
//