Removing a Specific Element from a Dictionary
When deleting data from a dictionary, you can choose the item to delete using the Key
.
To delete a specific key-value pair, use the del
keyword and specify the key to be deleted within the square bracket ([]).
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.