(Default)Dict

Dict

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_dict: dict[str, int] = dict()

my_dict["hallo"] = 2
my_dict.pop("falscherKey")        ## raise KeyError
my_dict.pop("falscherKey", None)  ## return None
my_dict.get("hallo", None)        ## return 2
my_dict.pop("hallo", None)        ## return 2 and remove "hallo",2 from dict

for name, value in my_dict.items():
    pass

Defaultdict

1
2
3
4
5
from collections import defaultdict

my_dict: defaultdict[str, int] = defaultdict(int)

my_dict["hallo"] += 2             ## doesn't find "hallo", create "hallo" with 0 and add 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9