http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Dictionaries
Pavan Verma
@YinYangPavan
Founder, P3 InfoTech Solutions Pvt. Ltd.
Python Programming Essentials
1
© SkillBrew http://skillbrew.com
What is a dictionary
2
• Dictionaries are collections of items that have
a “key” and a “value”
• Like a list is indexed by a number from 0 to len-
1, dictionaries are indexed by the keys
© SkillBrew http://skillbrew.com
Sample use cases of dictionary
 Storing a record of information as key-
value pairs
 Example: Contact list
3
© SkillBrew http://skillbrew.com
Creating a dictionary
4
mydict = {
'one': 1,
'two': 2,
}
print mydict
Output:
{'two': 2, 'one': 1}
Syntax
mydict = {
key1: value1,
key2: value2,
}
© SkillBrew http://skillbrew.com
Accessing elements of a dictionary
5
mydict = {
'one': 1,
'two': 2,
}
print mydict['one']
print mydict['two']
Output:
1
2
dict[key]
© SkillBrew http://skillbrew.com
Accessing elements of a dictionary (2)
6
mydict = {
'one': 1,
'two': 2,
}
print mydict['three']
Output:
KeyError: 'three' Accessing a key that does
not exist throws
KeyError
© SkillBrew http://skillbrew.com
Key types
7
Keys can be of any immutable type
1. Strings
2. Numbers
3. Tuples
© SkillBrew http://skillbrew.com
Key types (2)
8
mydict = {
'one': 1,
'two': 2,
}
Strings as keys
© SkillBrew http://skillbrew.com
Key types (3)
9
points = {
(1, 2): 10,
(3, 4): 20,
}
Tuples as keys
mydict = {
1: 'one',
2: 'two',
}
Numbers as keys
© SkillBrew http://skillbrew.com
Tuples as keys
10
• Tuples can be used as keys if they contain only other
immutable types – strings, numbers, or tuples
• If a tuple contains any mutable type, it cannot be used
as a key
mydict = {
([1, 2, 3], 4): 'one',
}
TypeError: unhashable type: 'list'
© SkillBrew http://skillbrew.com
Accessing keys
11
contact = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": True,
}
print contact.keys()
['salary', 'last', 'age', 'sex', 'registered',
'first']
dict.keys() gives an
unordered list of keys in a
dictionary
© SkillBrew http://skillbrew.com
Accessing values
12
contact = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": True,
}
print contact.values()
[70000, 'Doe', 39, 'M', True, 'John']
dict.values() gives an
unordered list of values in
dictionary
© SkillBrew http://skillbrew.com
Updating an element
13
contact = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": True,
}
contact['salary'] = 90000
print contact
Output:
{'salary': 90000, 'last': 'Doe', 'age': 39, 'sex': 'M',
'registered': True, 'first': 'John'}
dict[key] = newvalue
© SkillBrew http://skillbrew.com
Deleting an element
14
contact = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 90000,
"registered": True,
}
del contact['last']
print contact
Output:
{'salary': 90000, 'age': 39, 'sex': 'M', 'registered': True,
'first': 'John'}
del dict[key]
© SkillBrew http://skillbrew.com
Test if a key exists in a dictionary
15
contact = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 90000,
"registered": True,
}
print 'first' in contact
Output: True
print 'second' in contact
Output: False
Use in operator to test weather
a key exists or not
© SkillBrew http://skillbrew.com
Loop over a dictionary
16
for key in contact:
print key
Output:
salary
age
sex
registered
first
By default looping over a
dictionary, gives access to all the
keys
© SkillBrew http://skillbrew.com
Loop over a dictionary (2)
17
for key in contact.iterkeys():
print key
Output:
salary
age
sex
registered
first
dict.iterkeys():use this
to iterate over keys of a
dictionary
© SkillBrew http://skillbrew.com
Loop over a dictionary (3)
18
for key in contact.itervalues():
print key
Output:
90000
39
M
True
John
dict.itervalues(): use
this to iterate over values of a
dictionary
© SkillBrew http://skillbrew.com
Loop over a dictionary (4)
19
for key, value in contact.iteritems():
print key, value
Output:
salary 90000
age 39
sex M
registered True
first John
dict.iteritems(): use
this to iterate over both keys and
values
© SkillBrew http://skillbrew.com
Example: Contact information
contacts = [
{'name': 'Pavan Verma',
'mobile': '9999900000',
'email': 'pavan@p3infotech.in', }
{'name': 'Sahil Chug',
'mobile': '9999900001',
'email': 'sahil@p3infotech.in', }
{'name': 'Abijith MG',
'mobile': '9999900007',
'email': 'abijith@p3infotech.in', }
]
20
© SkillBrew http://skillbrew.com
Example: Contact information (2)
contacts = {
1: {'name': 'Pavan Verma',
'mobile': '9999900000',
'email': 'pavan@p3infotech.in', }
2: {'name': 'Sahil Chug',
'mobile': '9999900001',
'email': 'sahil@p3infotech.in', }
8: {'name': 'Abijith MG',
'mobile': '9999900007',
'email': 'abijith@p3infotech.in', }
}
21
© SkillBrew http://skillbrew.com
Summary
 What is a dictionary
 Creating a dictionary
 Accessing elements of a dictionary
 Key types
 Tuples as keys
 Iterating over dictionary
22
© SkillBrew http://skillbrew.com
References
 Dictionaries tutorial
http://www.tutorialspoint.com/python/python_dictionary.htm
 Dictionaries in detail in python docs
http://docs.python.org/2/library/stdtypes.html#mapping-
types-dict
23
24