📝 Edit page
➕ Add page
Dictionary
Mutable of object of key-value pairs.
Keys must be immutable so they can be hashed. e.g. str, int, tuple. Not a list or dict.
Related
- dict in the Python docs.
- dictionary type checks for how to annotate your dict and validate usage with type checking.
Create
{"a": 1, "b": 2}
dict(a=1, b=2)
Get
x['abc']
Implies returning None if key does not exist.
x.get('abc')
# None
x.get('abc', 0)
# 0
Set
x.update('abc', 1)
x['abc'] = 1
Delete
del x['abc']
Iterate
x.keys()
x.values()
x.items()