Dict literal
scores = {"alice": 92, "bob": 85}
dict() constructor
d = dict(name="Alice", age=28)
pairs = [("a", 1), ("b", 2)]
d = dict(pairs)
dict.fromkeys()
keys = ["x", "y", "z"]
d = dict.fromkeys(keys, 0)
Dict comprehension
squares = {x: x**2 for x in range(6)}
Keys must be hashable — str, int, float, and tuple work; list and dict do not.
Nesting
students = {
"alice": {"gpa": 3.9, "year": 2},
"bob": {"gpa": 3.4, "year": 3},
}
print(students["alice"]["gpa"])