Chapter 3: Dictionaries and Sets Flashcards

1
Q

What does ** do when applied to
- A function call
- The inside of a dict literal

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does | do when applied across two dictionaries? (Python 3.9+)

A

> > > 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is hashable?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is the hashcode of an object always the same?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the setdefault method to?

A

mydict.setdefault(key, default_value)
- returns value if key is in dict
- otherwise sets mydict[key] to default_value and returns default_value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why would we use a collections.defaultdict in place of a dict?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the __missing__ method do?

A

__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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why would one use a collections.OrderedDict in place of a dict?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why would one use a collections.ChainMap?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why would one use a collections.Counter?

A

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)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are dictionary views?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the notation for an empty set?

A

set()
NOT {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the set operations?

A

(&) intersection
(-) difference
(^) symmetric difference
(|) union

How well did you know this?
1
Not at all
2
3
4
5
Perfectly