dictionary1+python-merged
dictionary1+python-merged
Dictionaries
Dictionaries
Key Value
restroom bano
cat gato
boy muchacho
house casa
toad sapo
water agua
© A+ Computer Science -
Dictionary
Python dictionaries were built using
hash tables.
0 1 2 3 4
© A+ Computer Science -
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'three': 3, 'one': 1, 'two': 2, 'four': 4}
"two": 2,
"three": 3,
"four" : 4
Dictionaries store material as
} key:value
print( map )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1
"two": 2,
"three": 3,
"four" : 4
}
print( map["one"] )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1, 'two': 333}
"two": 2,
}
map["two"] = 333;
print( map )
© A+ Computer Science -
Dictionaries
map = {} OUTPUT
map[45]="at"; at
go
map[17]="go"
print( map[45] )
print( map[17] )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}
"two": 2,
}
del map["two"]
print( map )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}
"two": 2,
}
if "two" in map:
del map["two"]
print( map )
© A+ Computer Science -
© A+ Computer Science -
Dictionaries
frequently used methods
Name Use
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1
"two": 2,
}
print( map.get("one" ) )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, {'one': 1}
"two": 2,
}
map.pop("two")
print( map )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2
"two": 2,
}
print( len(map) )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, ('one', 1)
('two', 2)
"two": 2,
}
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2
1
"two": 2,
}
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}
© A+ Computer Science -
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}
for key in map :
print( key )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, one
two
"two": 2,
}
for x in map.keys() :
print( x )
Note: The previous slide did the exact same thing. When you loop
through a dictionary without calling a method, you loop through
the keys.
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 1
2
"two": 2,
} Using the keys as indexes, we can
for key in map : receive the values
print( map[key] )
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, 2
1
"two": 2,
}
© A+ Computer Science -
Dictionaries
map = { OUTPUT
"one": 1, ('one', 1)
('two', 2)
"two": 2,
}
© A+ Computer Science -
Building a Dictionary
map = {}
OUTPUT
{2: 5, 3: 2, 4: 1, 5: 1, 6: 1,
list = [3,4,5,6,7,8,2,2,2,2,2,3]
7: 1, 8: 1}
for x in list :
if not x in map : x is not in the dictionary
map[x] = 0 set the key to zero
map[x] = map[x] + 1 add one to the key
print( map )
© A+ Computer Science -
Example 1
Example 2
Example 3
Tuples
Tuples are lists that cannot be changed
print(myTuple)
Output
(255, 0, 89)
© A+ Computer Science -
Tuples
The data is accessed with indices
print(myTuple[2])
Output
89
© A+ Computer Science -
Tuples
Since tuples cannot be changed, they have no methods
myTuple.append(35)
© A+ Computer Science -
Immutable (not changeable)
Indexing/slicing
summary