August 2018
Intermediate to advanced
366 pages
10h 14m
English
MultiDict works by storing a list for each key. Whenever a key is accessed, the list containing all the values for that key is retrieved.
In the case of missing keys, an empty list will be provided so that values can be added for that key.
This works because every time defaultdict faces a missing key, it will insert it with a value generated by calling list. And calling list will actually provide an empty list. So, doing rd[v] will always provide a list, empty or not, depending on whether v was an already existing key or not. Once we have our list, adding a new value is just a matter of appending it.