Removing a Specific Element from a Dictionary
When deleting data from a dictionary, specify the item to remove using itskey
.
To remove a specific key-value pair, use the del
keyword with the key inside square brackets ([])
.
Removing a Value from a Dictionary
order = {
"product_id": 2,
"product_name": "Computer",
"quantity": 3,
"price": 1000
}
# Remove the "quantity" key and its value
del order["quantity"]
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.