Python Basics — Dictionary Methods
Dictionary Methods
Python dictionaries come with powerful built-in methods for accessing, iterating, and transforming data.
View Methods
.keys() — All Keys
user = {"name": "Alice", "age": 30, "city": "New York"}
print(user.keys())
# dict_keys(['name', 'age', 'city'])
# Convert to list
print(list(user.keys())) # ['name', 'age', 'city']
.values() — All Values
print(user.values())
# dict_values(['Alice', 30, 'New York'])
.items() — All Key-Value Pairs
print(user.items())
# dict_items([('name', 'Alice'), ('age', 30), ('city', 'New York')])
These return view objects that reflect changes to the dictionary in real time.
.update() — Merge Dictionaries
user = {"name": "Alice", "age": 30}
# Update with another dict
user.update({"age": 31, "email": "alice@example.com"})
print(user)
# {'name': 'Alice', 'age': 31, 'email': 'alice@example.com'}
# Update with keyword arguments
user.update(city="New York")
print(user)
# {'name': 'Alice', 'age': 31, 'email': 'alice@example.com', 'city': 'New York'}
.clear() — Remove All Items
d = {"a": 1, "b": 2, "c": 3}
d.clear()
print(d) # {}
.copy() — Shallow Copy
original = {"a": 1, "b": [2, 3]}
copy = original.copy()
copy["a"] = 99
print(original["a"]) # 1 (unchanged)
# But nested objects are shared!
copy["b"].append(4)
print(original["b"]) # [2, 3, 4] (changed!)
Iterating Over Dictionaries
Keys (default)
user = {"name": "Alice", "age": 30, "city": "New York"}
for key in user:
print(key)
# name
# age
# city
Values
for value in user.values():
print(value)
# Alice
# 30
# New York
Key-Value Pairs
for key, value in user.items():
print(f"{key}: {value}")
# name: Alice
# age: 30
# city: New York
Merging Dictionaries
Using | Operator (Python 3.9+)
defaults = {"theme": "light", "font_size": 14}
custom = {"font_size": 16, "language": "en"}
merged = defaults | custom
print(merged)
# {'theme': 'light', 'font_size': 16, 'language': 'en'}
The right side wins for duplicate keys.
Using |= (In-place Merge)
defaults = {"theme": "light", "font_size": 14}
defaults |= {"font_size": 16, "language": "en"}
print(defaults)
# {'theme': 'light', 'font_size': 16, 'language': 'en'}
Using ** Unpacking
defaults = {"theme": "light", "font_size": 14}
custom = {"font_size": 16, "language": "en"}
merged = {**defaults, **custom}
print(merged)
# {'theme': 'light', 'font_size': 16, 'language': 'en'}
Using .update()
merged = defaults.copy()
merged.update(custom)
Complete Methods Reference
| Method | Description | Returns |
|---|---|---|
d.keys() |
All keys | dict_keys view |
d.values() |
All values | dict_values view |
d.items() |
Key-value pairs | dict_items view |
d.get(k, default) |
Value for key | Value or default |
d.setdefault(k, v) |
Get or set | Value |
d.update(other) |
Merge in-place | None |
d.pop(k, default) |
Remove and return | Value |
d.popitem() |
Remove last pair | (key, value) tuple |
d.clear() |
Remove all | None |
d.copy() |
Shallow copy | New dict |
d.fromkeys(keys, v) |
New dict from keys | New dict |
Summary
.keys(),.values(),.items()return live view objects.update()merges another dict (or kwargs) in-place|operator merges dicts (Python 3.9+), right side wins.copy()creates a shallow copy (nested objects shared)- Iterate with
for key, value in d.items():
Next up: Dictionary Comprehensions →