Dictionaries
CS100/CS101
Outline
●
Dictionary
●
Dictionary Comprehension
●
Operations, View
Dictionary
●
Sequences are indexed by a range of numbers
●
Dictionary
●
Sequences are indexed by a range of numbers
●
Dictionaries are indexed by keys
– Keys can be strings and numbers
●
Dictionary
●
Sequences are indexed by a range of numbers
●
Dictionaries are indexed by keys
– Keys can be strings and numbers
●
Dictionary is a set of key: value pairs
– keys are unique in a dictionary
Dictionary
>>>
>>> aa == dict(one=1,
dict(one=1, two=2,
two=2, three=3)
three=3)
>>>
>>> b = {'one': 1, 'two': 2, 'three':
b = {'one': 1, 'two': 2, 'three': 3}
3}
>>>
>>> c = dict(zip(['one', 'two', 'three'], [1,
c = dict(zip(['one', 'two', 'three'], [1, 2,2, 3]))
3]))
>>> d = dict([('two', 2), ('one', 1), ('three',
>>> d = dict([('two', 2), ('one', 1), ('three', 3)]) 3)])
>>>
>>> ee == dict({'three':
dict({'three': 3, 3, 'one':
'one': 1,
1, 'two':
'two': 2})
2})
>>> f = dict({'one': 1, 'three':
>>> f = dict({'one': 1, 'three': 3}, two=2)3}, two=2)
>>>
>>> aa ==
== bb ==
== cc ==
== dd ==
== ee ==
== ff
Dictionary
>>>
>>> tel
tel == {'jack':
{'jack': 4098,
4098, 'sape':
'sape': 4139}
4139}
>>> tel['guido'] =
>>> tel['guido'] = 41274127
>>>
>>> tel
tel
{'jack':
{'jack': 4098,
4098, 'sape':
'sape': 4139,
4139, 'guido':
'guido': 4127}
4127}
>>>
>>> tel['jack']
tel['jack']
4098
4098
>>>
>>> del
del tel['sape']
tel['sape']
>>>
>>> tel['irv']
tel['irv'] == 4127
4127
>>>
>>> tel
tel
{'jack':
{'jack': 4098,
4098, 'guido':
'guido': 4127,
4127, 'irv':
'irv': 4127}
4127}
>>>
>>> list(tel)
list(tel)
['jack',
['jack', 'guido',
'guido', 'irv']
'irv']
>>>
>>> sorted(tel)
sorted(tel)
['guido',
['guido', 'irv',
'irv', 'jack']
'jack']
>>>
>>> 'guido'
'guido' inin tel
tel
True
True
>>>
>>> 'jack'
'jack' not
not in
in tel
tel
False
False
Dictionary Comprehension
>>>
>>> {x:
{x: x**2
x**2 for
for xx in
in (2,
(2, 4,
4, 6)}
6)}
{2:
{2: 4,
4, 4:
4: 16,
16, 6:
6: 36}
36}
Operations - Dictionary
>>>
>>> list(d)
list(d)
>>>
>>> len(d)
len(d)
>>>
>>> d[key]
d[key]
>>>
>>> d.get(key)
d.get(key)
>>>
>>> d[key]
d[key] == value
value
>>>
>>> key
key in
in dd
>>>
>>> key
key not
not in
in dd
Operations - Dictionary
>>>
>>> iter(d)
iter(d) ## shortcut
shortcut for
for iter(d.keys())
iter(d.keys())
>>>
>>> d.clear()
d.clear()
>>>
>>> d.items()
d.items()
>>>
>>> d.pop(key)
d.pop(key)
>>>
>>> reversed(d)
reversed(d) ## shortcut
shortcut for
for reversed(d.keys())
reversed(d.keys())
>>>
>>> d.update(list_of_key_value_pairs)
d.update(list_of_key_value_pairs) ## d.update(red=1,
d.update(red=1, blue=2)
blue=2)
Dictionary View
>>>
>>> dishes
dishes == {'eggs':
{'eggs': 2,
2, 'sausage':
'sausage': 1,
1, 'bacon':
'bacon': 1,
1, 'spam':
'spam': 500}
500}
>>> keys = dishes.keys()
>>> keys = dishes.keys()
>>>
>>> values
values == dishes.values()
dishes.values()
>>>
>>> ## keys
keys and
and values
values are
are iterated
iterated over
over in
in the
the same
same order
order (insertion
(insertion order)
order)
>>> list(keys)
>>> list(keys)
>>>
>>> list(values)
list(values)
Dictionary
●
mapping type data structure
– associates hashable keys to values
●
Value 2
key1
key2
Value 1
Dictionary
●
Keys must be hashable and unique across the
dictionary
– hash()
●
keys are immutable - once added to a dict, they
can only be removed, they cannot be updated
Dictionary
●
Values: any or multiple data type(s) or
structures, including other dictionaries
Dictionary
●
Enable retrieval of a value in constant O(1)
time, given the key.
– vs. O(n) in Linear search
●
Useful where collection of items is large and
must be accessed and/or updated frequently
Appendix
hashable
●
An object is hashable if it has a hash value
which never changes during its lifetime and can
be compared to other objects
●
Hashable objects which compare equal must
have the same hash value.
>>>
>>> x=10
x=10
>>>
>>> hash(x)
hash(x)
Summary
●
Dictionary
●
Dictionary Comprehension
●
Operations, View