Chapter 3: Dictionaries and Sets Flashcards
What does ** do when applied to
- A function call
- The inside of a dict literal
Unpacking
»> def dump(kwargs):
… return kwargs
…
»> dump({‘x’: 1}, y=2, **{‘z’: 3})
{‘x’: 1, ‘y’: 2, ‘z’: 3}
> > > {‘a’: 0, **{‘x’: 1}, ‘y’: 2, **{‘z’: 3, ‘x’: 4}}
{‘a’: 0, ‘x’: 4, ‘y’: 2, ‘z’: 3}
What does | do when applied across two dictionaries? (Python 3.9+)
> > > d1 = {‘a’: 1, ‘b’: 3}
d2 = {‘a’: 2, ‘b’: 4, ‘c’: 6}
d1 | d2
{‘a’: 2, ‘b’: 4, ‘c’: 6}
We can also use |= to update an existing mapping in place
What is hashable?
An object whos hash code never changes during it’s lifetime (has __hash__ method)
AND
that can be compared to other objects (has __eq__ method)
Tuples that contain lists cannot be hashed, for containers all contained objects must be hashable
Is the hashcode of an object always the same?
The hashcode of an object may be different depending on
- the version of Python
- the machine architecture
- the salt added to the hash computation
A hash code is only guaranteed to be constant within one Python process
What does the setdefault method to?
mydict.setdefault(key, default_value)
- returns value if key is in dict
- otherwise sets mydict[key] to default_value and returns default_value
Why would we use a collections.defaultdict in place of a dict?
- Instantiated with a callable, default factory
e.g collections.defaultdict(list) - If __getitem__ is called with a non-existent key it creates a value using the callable and assigns the result/a reference to the result as a value
What does the __missing__ method do?
__missing__(key) is called by the interpreter when a key is not found in the dictionary
It is not defined in the base dict class, but it is aware of it and will be called in a user-defined subclass of dict if defined
Why would one use a collections.OrderedDict in place of a dict?
An OrderedDict preserves the ordering of the keys. As of Python3.6 a dict does this also however there are minor differences.
e.g OrderedDict checks for matching order when checking equality
Why would one use a collections.ChainMap?
A ChainMap instance holds a list of mappings that can be searched as one
The ChainMap does not copy the input mappings, only holds references to them
Updates and insertions only affect the first mapping
Useful for implementing languages with nested scoped
Why would one use a collections.Counter?
Holds an integer count for each key
Updating a key adds to it’s count
> > > ct = collections.Counter(‘abracadabra’)
ct
Counter({‘a’: 5, ‘b’: 2, ‘r’: 2, ‘c’: 1, ‘d’: 1})
ct.update(‘aaaaazzz’)
ct
Counter({‘a’: 10, ‘z’: 3, ‘b’: 2, ‘r’: 2, ‘c’: 1, ‘d’: 1})
ct.most_common(3)
[(‘a’, 10), (‘z’, 3), (‘b’, 2)]
What are dictionary views?
Read-only projections of the internal data structures used in the dict implementation. e.g dict_keys, dict_values and dict_items
They do not copy the data into a list as was done in Python2 and they update as the data updates
What is the notation for an empty set?
set()
NOT {}
What are the set operations?
(&) intersection
(-) difference
(^) symmetric difference
(|) union