Blame
|
1 | # (Default)Dict |
||||||
| 2 | ||||||||
| 3 | ## Dict |
|||||||
| 4 | ```python= |
|||||||
| 5 | my_dict: dict[str, int] = dict() |
|||||||
| 6 | ||||||||
| 7 | my_dict["hallo"] = 2 |
|||||||
| 8 | my_dict.pop("falscherKey") ## raise KeyError |
|||||||
| 9 | my_dict.pop("falscherKey", None) ## return None |
|||||||
| 10 | my_dict.get("hallo", None) ## return 2 |
|||||||
| 11 | my_dict.pop("hallo", None) ## return 2 and remove "hallo",2 from dict |
|||||||
| 12 | ||||||||
| 13 | for name, value in my_dict.items(): |
|||||||
| 14 | pass |
|||||||
| 15 | ``` |
|||||||
| 16 | ||||||||
| 17 | ## Defaultdict |
|||||||
| 18 | ```python= |
|||||||
| 19 | from collections import defaultdict |
|||||||
| 20 | ||||||||
| 21 | my_dict: defaultdict[str, int] = defaultdict(int) |
|||||||
| 22 | ||||||||
| 23 | my_dict["hallo"] += 2 ## doesn't find "hallo", create "hallo" with 0 and add 2 |
|||||||
| 24 | ``` |
|||||||
