📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Dictionaries

Dictionaries

5 min read Quiz at the end
Dictionaries store key-value pairs: {'name': 'Alice', 'age': 30}. Access with dict['key'] or dict.get('key', default). Iterate with .items() for both keys and values. Ordered from Python 3.7 onwards.

Python Dictionaries

user = {"name": "Alice", "age": 28, "active": True}

print(user["name"])          # Alice
print(user.get("email", "")) # safe access

user["email"] = "a@b.com"   # add/update
del user["active"]

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

# Dict comprehension
squares = {x: x**2 for x in range(5)}
Sign in to track your progress.
Topic Quiz · 5 questions

Test your understanding before moving on

1. How do you safely get a value with a default?
💡 .get(key, default) returns the default instead of raising KeyError.
2. Which method returns all key-value pairs?
💡 .items() returns dict_items of (key, value) tuples.
3. Are dictionary keys ordered in Python 3.7+?
💡 From Python 3.7, dictionaries maintain insertion order as an implementation detail.
4. Which creates a dict from two lists?
💡 dict(zip(keys, values)) creates a dictionary from two parallel lists.
5. What does del dict[key] do?
💡 del dict[key] permanently removes the key and its value.