Dictionaries
Key–value maps with fast lookup — ideal for configurations, counters and JSON data.
Beginner
resistors = {"R1": 220, "R2": 10_000}
resistors["R3"] = 4_700 # add
resistors.get("R9", 0) # default if missing
for name, ohms in resistors.items():
print(name, ohms)
Lookups take roughly constant time thanks to hashing. Keys must be immutable (strings, numbers, tuples). Since Python 3.7, dicts keep insertion order.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.