Table of contents
INTRODUCTION: Dictionary
Key-value pair
Creating A Dictionary
Iterating Through A Dictionary
Deleting Dictionary Elements
Built-in Dictionary Functions
INTRODUCTION: Dictionary
A dictionary is like a list, but more in general. In a list, index value is an integer, while in a
dictionary index value can be any other data type and are called keys. The key will be used
as a string as it is easy to recall. A dictionary is an extremely useful data storage construct
for storing and retrieving all key value pairs, where each element is accessed (or indexed) by
a unique key. However, dictionary keys are not in sequences and hence maintain no left-to
right order.
It is an un-ordered collection of items where each item consist of a key and a value. It is
mutable (can modify its contents ) but Key must be unique and immutable. Dictionary is also
known as associative array or mapping or hashes .
Key-value pair
We can refer to a dictionary as a mapping between a set of indices (which are called keys)
and a set of values. Each key maps a value. The association of a key and a value is called a
key-value pair.
Syntax:
my_dict = {‘key1’: ‘value1’,‘key2’: ‘value2’,‘key3’: ‘value3’…‘keyn’: ‘valuen’}
Note: Dictionary is created by using curly brackets(ie. {}).
Example
A={1:"one",2:"two",3:"three"}
print A
output:
{1: ‘one’, 2: ‘two’, 3: ‘three’}
Creating A Dictionary
It is enclosed in curly braces {} and each item is separated from other item by a comma(,).
Within each item, key and value are separated by a colon (:).
Example: -
dict = {‘Subject': ‘Informatic Practices', 'Class': ‘11'}
#Accessing an Item
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print(dict)
print ("Subject : ", dict['Subject'])
print ("Class : ", dict.get('Class'))
OUTPUT
{‘Class’: ‘11’, ‘Subject’: ‘Informatics Practices’} (’Subject : ’, ‘Informatics Practices’) (’Class :
’, 11)
Iterating Through A Dictionary
Following example will show how dictionary items can be accessed through loop.
Example: -
dict = {'Subject': 'Informatics Practices', 'Class': 11}
for i in dict:
print(dict[i])
OUTPUT
Informatics practices 11
Updating Dictionary Elements
We can change the individual element of dictionary.
Example: -
dict = {'Subject': 'Informatics Practices', 'Class': 11}
dict['Subject']='computer science'
print(dict)
OUTPUT
{‘Class’: 11, ‘Subject’: ‘computer science’}
Deleting Dictionary Elements
del, pop() and clear() statement are used to remove elements from the dictionary.
del
Example: -
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print('before del', dict)
del dict['Class'] # delete single element
print('after item delete', dict)
del dict #delete whole dictionary
print('after dictionary delete', dict)
Output
(‘before del’, {‘Class’: 11, ‘Subject’: ‘Informatics Practices’}) (‘after item delete’, {‘Subject’:
‘Informatics Practices’}) (‘after dictionary delete’, )
pop() method is used to remove a particular item in a dictionary. clear() method is used to
remove all elements from the dictionary.
Example: -
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print('before del', dict)
dict.pop('Class')
print('after item delete', dict)
dict.clear()
print('after clear', dict)
Output
(‘before del’, {‘Class’: 11, ‘Subject’: ‘Informatics Practices’}) (‘after item delete’, {‘Subject’:
‘Informatics Practices’}) (‘after clear’, {})
Built-in Dictionary Functions
len(dict) Gives the total length of the dictionary. It is equal to the number of items in the
dictionary. str(dict) Return a printable string representation of a dictionary type(variable) If
variable is dictionary, then it would return a dictionary type.
Some more examples of Dictionary are:-
Dict1= { } # this is an empty dictionary without any element.
DayofMonth= { January”:31, ”February”:28, ”March”:31, ”April”:30, ”May”:31, ”June”:30,
”July”:31, ”August”:31, ”September”:30, ”October”:31, ”November”:30, ”December”:31}
FurnitureCount = { “Table”:10, “Chair”:13, “Desk”:16, “Stool”:15, “Rack”:15 }
By above examples you can easily understand about the keys and their values. One thing to
be taken care of is that keys should always be of immutable type.