Tag: Dictionaries in Python

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!

Python Dictionaries

A dictionary is a set of unordered key, value pairs. In a dictionary, the keys must be unique and they are stored in an unordered manner.

In this tutorial you will learn the basics of how to use the Python dictionary.

Creating a Dictionary:

dict1

Accessing Items:
You can access the items of a dictionary by referring to its key name, inside square brackets:

dict2

Updating Dictionary:
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −

dict3

Loop Through a Dictionary:
You can loop through a dictionary by using a for loop.When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

dict4.JPG

Check if Exists:

You can test the presence of a key using ‘in’ or ‘not in’

dict5

Restrictions on Dictionary Keys:
Almost any type of value can be used as a dictionary key in Python. As an example,  integer, float, and Boolean objects are used as keys:

dict6

However, there are a couple restrictions that dictionary keys must abide by.

First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once.
You could see below that when you assign a value to an already existing dictionary key, it does not add the key a second time, but replaces the existing value:

dict7

Restrictions on Dictionary Values:
By contrast, there are no restrictions on dictionary values. Literally none at all. A dictionary value can be any type of object Python supports, including mutable types like lists and dictionaries, and user-defined objects.There is also no restriction against a particular value appearing in a dictionary multiple times.

Hope you have enjoyed the post. Keep reading