Example 1 — Five Ways to Build a Dictionary
Create dicts from keywords, pairs, zip(), another dict with overrides, and nothing.
a = dict(name="Ana", age=30)
b = dict([("x", 1), ("y", 2)])
c = dict(zip(["red", "green"], ["#f00", "#0f0"]))
d = dict(a, age=31, city="Oslo")
print(a)
print(b)
print(c)
print(d)
print(dict()) How It Works
- Keywords become string keys.
dict(name="Ana", age=30)is the same as the literal{"name": "Ana", "age": 30}. - Pairs and zip. Any iterable of two-item pairs works, including
zip(keys, values). - Copy with overrides.
dict(a, age=31, city="Oslo")copiesa, replaces age, and adds city at the end. - Order kept. Updated keys keep their original position; new keys go last.

