Python Dictionary
Python Dictionary
Python Dictionary
A dictionary is a collection of pair of values. A pair is also called an element of the dictionary. All the
pairs (elements) in the dictionary are enclosed within {}. Every pair has two components separated by
colon (:). The value on the left-hand side of the colon (:) is the key. A key is immutable type like int or
float or str. Key can either be a constant or a variable. The value on the right-hand side of the colon (:) is
the value for the key. Value for the value can either be a constant or a variable or an expression. A
dictionary is a mutable type. In a dictionary key acts like an index for every element in the dictionary.
How to create a dictionary?
Dictionary d1 is created with 3 pairs. Key for every pair is an int type. Every value in the dictionary d1
is a constant. Dictionary d2 is created with 3 pairs. Key for every pair is a float type. Every value in the
dictionary d2 is a constant. Dictionary d3 is created with 3 pairs. Key for every pair is a str type. Every
value in the dictionary d3 is a constant. Dictionary d4 is created with 4 pairs. Key for every pair is a str
type. Values in the first 3 pairs are variable and value for the last pair is an expression. Dictionary ds is
created where keys are string (str) type variable and values are also variables. Dictionary d6 is created
as an empty dictionary (without any pair).
>>> print(d1)
Displays {1: 1011, 2:'ABIJEET', 3: 78000.0}
>>> print(d2)
Displays {1.5: 1012, 2.5: 'SUNIL', 3.5: 75000.0}
>>> print(d3)
Displays {'eno': 1013, 'name': 'KARAN', 'bsal': 77000.0}
>>> print(d4)
Displays {'eno': 1014, 'name': 'DEEPAK', 'bsal': 76000.0, 'hra': 15200.0}
>>> print(d5)
Displays {'code': 1027, 'name': 'RADHIKA SINHA', 'bsal': 156000.0}
>>> print(d6)
Displays {}
Keys in a dictionary are supposed to be distinct. But what happens when a key is repeated?
>>> stu={'rn':10, 'na':'TARUN', 'th':50, 'pr':25, 'th':55}
>>> print(stu)
Displays {'rn':10, 'na':'TARUN', 'th':55, 'pr':25}
In the dictionary stu, key 'th' is repeated twice. When a key is repeated, Python will take the last key
from the left and ignore the previous key(s). As displayed above, first 'th':50 is ignored but second
'th':55 is accepted.
FAIPS, DPS Kuwait Page 1 of 8 © Bikram Ally
Python Notes Class XI Dictionary
Function print(dictname) will display the entire dictionary. To display only the keys in dictionary
we can use keys() method of dictionary. Method keys() will display the keys of the dictionary as a
list. To display the only values in dictionary we can use values() method of dictionary. Method
values() will display the values of the dictionary as a list.
>>> print(d1.keys())
Displays dict_keys([1, 2, 3])
>>> print(d2.keys())
Displays dict_keys([1.5, 2.5, 3.5])
>>> print(d3.keys())
Displays dict_keys(['eno', 'name', 'bsal'])
>> print(d4.keys())
Displays dict_keys(['eno', 'name', 'bsal', 'hra'])
>> print(d5.keys())
Displays dict_keys(['code', 'name', 'bsal'])
>>> print(d6.keys())
Displays dict_keys([])
>>> print(d1.values())
Displays dict_values([1011, 78000.0])
>>> print(d2.values())
Displays dict_values([1012, 'SUNIL', 75000.0])
>>> print(d3.values())
Displays dict_values([1013, 'KARAN', 77000.0])
>>> print(d4.values())
Displays dict_values([1014, 'DEEPAK', 76000.0, 15200.0])
>>> print(d5.values())
Displays dict_values(1027, 'RADHIKA SINHA', 156000.0])
>>> print(d6.values())
Displays dict_values([])
As mentioned earlier, a key in a dictionary can either be int or float or str or any other immutable type.
Normally, by convention key is a str type. We will follow the convention and use str type for a key.. To
access a particular value in a pair (element of a dictionary), we need the key for that pair since key acts as
an index for every pair in the dictionary. A value in a dictionary is accessed as: