Python Dict Methods

About This Index
This page is the Python Dict Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example prints fixed data, and since dicts keep insertion order, the output you see is the output you get.
Learn what keys can be, how ordering works, and the classic mistakes on the Dict hub. Use this page when you need a specific method such as get(), setdefault(), or items().
Every method below belongs to the built-in dict type, so no import is needed. All are called on a dict object except fromkeys(), a classmethod called as dict.fromkeys(...). The Returns column tells you whether you get a value, a live view, a new dict, or None.
How to Call a Dict Method
Write the dict, a dot, the method name, and the arguments. Some methods return a value or a live view; update() and clear() only change the dict and return None:
stock = {"apples": 5, "pears": 2}
print(stock.get("plums", 0)) # 0
stock.update(plums=7) # add or overwrite keys
print(stock.pop("pears")) # 2
print(list(stock.keys())) # ['apples', 'plums']
print(stock.setdefault("figs", 1)) # 1
print(stock) # {'apples': 5, 'plums': 7, 'figs': 1} Python Dict Methods Table
All method guides in this Python tutorial series (11 tutorials), grouped by purpose.
Read keys, values, and items
4 methodsLook up one value safely, or get a live view of every key, every value, or every key/value pair for loops and membership tests.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
get() | value or default | Return the value for a key, or a default instead of raising KeyError. | Try it |
keys() | dict_keys view | Return a live, set-like view of the keys of a dict. | Try it |
values() | dict_values view | Return a live view of the values of a dict, for sums, counts, and checks. | Try it |
items() | dict_items view | Return a live view of (key, value) pairs for loops, sorting, and comparisons. | Try it |
Add and update entries
3 methodsMerge other data into a dict, insert a default only when a key is missing, or build a new dict from a list of keys.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
update() | None | Merge keys from another dict, a list of pairs, or keyword arguments, in place. | Try it |
setdefault() | value for key | Return the value of a key, inserting a default first if the key is missing. | Try it |
fromkeys() | new dict | Build a new dict from a list of keys, all set to the same value. | Try it |
Remove entries
3 methodsTake out one key and get its value back, take out the most recently inserted pair, or empty the whole dict in place.
Copy a dict
1 methodMake a new, independent top-level dict that still shares nested lists and dicts with the original.
Dict Operators and Idioms
These are not named methods, but you will use them next to the methods above in almost every program:
| Code | What it does |
|---|---|
d[key] | Read the value for key; raises KeyError if the key is missing. |
d[key] = value | Add the key, or overwrite its value; an existing key keeps its position. |
del d[key] | Remove the key; raises KeyError if it is missing. |
key in d | True if the key exists (checks keys only, never values). |
len(d) | Number of key/value pairs. |
for key in d: | Loop over the keys in insertion order. |
d | other | New merged dict; values from other win (Python 3.9+). |
d |= other | Merge other into d in place, like update() (Python 3.9+). |
ages = {"Tom": 31}
ages["Uma"] = 27 # add a key
print(ages["Tom"]) # 31
print("Uma" in ages) # True
del ages["Tom"] # remove a key
print(len(ages)) # 1
merged = ages | {"Vic": 40} # new dict (3.9+)
ages |= {"Uma": 28} # update in place (3.9+)
print(merged, ages) # {'Uma': 27, 'Vic': 40} {'Uma': 28} Use get() instead of d[key] when a missing key is normal, and pop() instead of del when you also need the removed value. On Python 3.8, write {**ages, "Vic": 40} instead of |.
Frequently Asked Questions
Start with clear()
Open the first guide in the list and work your way through the dict methods.
15 people found this page helpful
