Python Dictionaries – del, clear

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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s