This is a continuation of Kiran’s Python Dictionaries post. There were few online and offline questions, so thought of writing a post on del and clear commands on Python Dictionary.
Few Dictionary operations
To better explain, let us create a sample dictionary for our explanation.
fruit= {"ORange":"I love ORange",
"Apple":"Apple is good for health"}
print ("Entire defined dictionary values")
fruit
1. How to Delete a key from Dictionary
fruit= {"ORange":"I love ORange",
"Apple":"Apple is good for health"}
#Delete command is as below
print ("Delete a single element")
del fruit["Apple"]
fruit
2. How to Delete entire Dictionary
fruit= {"ORange":"I love ORange",
"Apple":"Apple is good for health"}
print ('Delete the dictionary')
del fruit
fruit
3. How to Clear entire Dictionary
fruit= {"ORange":"I love ORange",
"Apple":"Apple is good for health"}
print ('Clear the dictionary elements')
fruit.clear()
fruit
If you enjoyed this blog post, feel free to share it with your friends!