This document discusses dictionaries in Python. It describes dictionaries as mappings between keys and values where keys can be any immutable data type and values can be any data type. It covers creating dictionaries using the dict() function and literal notation, accessing values using keys or getting default values, and various dictionary methods like keys(), values(), and items(). Examples demonstrate using dictionaries to count word frequencies and invert a dictionary to map values to a list of keys. The document also discusses using tuples as dictionary keys.
2. objectives
1. Describe the characteristics of the
dictionary data structure in Python
2. Perform basic operations with
dictionaries including creation,
copying, updating, and traversing
3. Use dictionaries in functions
Vibrant Technology Course in Mumbai2
3. The dictionary data structure
In Python, a dictionary is
mapping between a set of indices
(keys) and a set of values
The items in a dictionary are key-
value pairs
Vibrant Technology Course in Mumbai3
4. The dictionary data structure
Keys can be any Python data
type
Because keys are used for indexing,
they should be immutable
Values can be any Python data
type
Values can be mutable or
immutable
Vibrant Technology Course in Mumbai4
6. Creating a dictionary
eng2sp = {'one': 'uno', 'two': 'dos',
'three': 'tres'}
print eng2sp
• In general, the order of items in a
dictionary is unpredictable
• Dictionaries are indexed by keys, not
integers
Vibrant Technology Course in Mumbai6
8. Dictionary indexing
if 'five' in eng2sp:
print eng2sp['five']
print eng2sp.get('five')
Vibrant Technology Course in Mumbai8
9. The in operator
• Note that the in operator works differently
for dictionaries than for other sequences
• For offset indexed sequences (strings, lists,
tuples), x in y checks to see whether x is an
item in the sequence
• For dictionaries, x in y checks to see whether
x is a key in the dictionary
Vibrant Technology Course in Mumbai9
10. Keys and values
The keys method returns a list of the
keys in a dictionary
print eng2sp.keys()
The values method returns a list of
the values
print eng2sp.values()
Vibrant Technology Course in Mumbai10
11. Keys and values
The items method returns a list of
tuple pairs of the key-value pairs in a
dictionary
print eng2sp.items()
Vibrant Technology Course in Mumbai11
12. Example: histogram.py
def histogram(seq):
d = dict()
for element in seq:
if element not in d:
d[element] = 1
else:
d[element] += 1
return d
h = histogram('brontosaurus')
print h
Vibrant Technology Course in Mumbai12
13. Example: histogram2.py
Add the following code to histogram.py:
def print_hist(hist):
for key in hist:
print key, hist[key]
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai13
14. Example: histogram2.py
Change the print_hist function:
def print_hist(hist):
for key, value in hist:
print key, value
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai14
15. Sorting the keys
Change the print_hist function:
def print_hist(hist):
keys = hist.keys()
keys.sort()
for key in keys:
print key, hist[key]
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai15
16. Using lists as values: invert.py
Add the following code to histogram.py:
def invert_dict(d):
inv = dict()
for key in d:
val = d[key]
if val not in inv:
inv[val] = [key]
else:
inv[val].append(key)
return inv
Vibrant Technology Course in Mumbai16
17. Using lists as values: invert.py
Add the following code to histogram.py:
hist = histogram('parrot')
print hist
inverted = invert_dict(hist)
print inverted
Vibrant Technology Course in Mumbai17
18. Using tuples as keys: troupe.py
troupe = {('Cleese', 'John'): [1,2,3],
('Chapman', 'Graham'): [4,5,6],
('Idle', 'Eric'): [7,8,9],
('Jones', 'Terry'): [10,11,12],
('Gilliam', 'Terry'): [13,14,15,16,17,18],
('Palin', 'Michael'): [19,20]}
for last, first in troupe:
print first, last, troupe[last, first]
Vibrant Technology Course in Mumbai18
19. Thank You…
Vibrant Technology Course in Mumbai19
Vibrant Technologies & Computers
www.vibranttechnologies.co.in
“The Best Training Center in Mumbai”