Skip to content

Dictionary

In one line

A dictionary is a collection of key-value pairs in Python, written as {"key": value}, for fast lookups by key.

In simple words

A Python dictionary (dict) stores data as key-value pairs. You look up a value by its key, like a real dictionary where you find a definition by the word. Dictionaries are written with curly braces: {"name": "Ada", "age": 36}.

Dictionaries are the Python equivalent of JavaScript objects or Java HashMaps. Keys must be unique and hashable (strings, numbers, tuples — but not lists). Values can be anything. Lookups, insertions, and deletions are all fast — close to constant time on average.

Dictionaries are everywhere in Python. JSON data maps directly to dicts, configuration files load into dicts, and many APIs return dicts. You iterate over keys, values, or both with .keys(), .values(), and .items().

Example

python
user = {"name": "Ada", "age": 36, "role": "admin"}

print(user["name"])      # "Ada"
print(user.get("email", "N/A"))  # "N/A" (key missing)

user["email"] = "[email protected]"  # add a key

for key, value in user.items():
    print(f"{key}: {value}")

Dictionaries support key access, .get() with a default, adding keys, and iterating with .items().

Related terms

Related Resources

Related Lessons

  • Data Types

    Dictionaries are a core Python data type

Related Tools

Learn the fundamentals

Deepen your understanding with structured lessons on this topic.

View lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary