June 2019
Beginner to intermediate
770 pages
19h 24m
English
An ordinary dict type raises an exception when a key is not found. A defaultdict collection class does this differently. Instead of raising an exception, it evaluates a given function and inserts the value of that function into the dictionary as a default value.
A common use case for the defaultdict class is to create indices for objects. When several objects have a common key, we can create a list of objects that share this key.
Here's a function that accumulates a list of distinct values based on a summary of the two values:
from typing import Dict, List, Tuple, DefaultDictdef dice_examples(n: int=12, seed: Any= ...
Read now
Unlock full access