Commit 499e9b
2026-08-06 08:13:49 Niklas Polke: Create (Default)Dict| /dev/null .. python/(default)dict.md | |
| @@ 0,0 1,24 @@ | |
| + | # (Default)Dict |
| + | |
| + | ## Dict |
| + | ```python= |
| + | 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 |
| + | ```python= |
| + | 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 |
| + | ``` |
