Dictionary methods · Python Reference

Dictionaries map keys to values, and their methods let you read safely, add, merge and remove entries, and loop over keys, values or pairs. Each page shows the syntax, the return value, a runnable example and the mistakes that cause KeyError and surprising results.

All 11

Python Dictionary get() Method
`dict.get(key, default=None)` returns the value for `key` if it exists, otherwise `default` (which is `None` unless you pass some…
Python Dictionary items() Method
`dict.items()` returns a live view of the dictionary's `(key, value)` pairs, most often used as `for key, value in d.items():` to…
Python Dictionary keys() Method
`dict.keys()` returns a live, read-only view of all the keys in a dictionary, in insertion order. Convert it with `list()` when y…
Python Dictionary values() Method
`dict.values()` returns a live view of all the values stored in a dictionary, in insertion order, ready to loop over or pass to `…
Python Dictionary update() Method
`dict.update(other)` copies every key-value pair from `other` into the dictionary, overwriting keys that already exist, changes t…
Python Dictionary pop() Method
`dict.pop(key[, default])` removes `key` from the dictionary and returns its value. If the key is missing it returns `default`, o…
Python Dictionary popitem() Method
`dict.popitem()` removes the most recently inserted key-value pair from a dictionary and returns it as a `(key, value)` tuple. On…
Python Dictionary setdefault() Method
`dict.setdefault(key, default=None)` returns the value for `key` if it exists; otherwise it stores `default` under `key` and retu…
Python Dictionary clear() Method
`dict.clear()` removes all key-value pairs from a dictionary in place, leaving it empty, and returns `None`. Every variable that …
Python Dictionary copy() Method
`dict.copy()` returns a new dictionary with the same keys and values. It is a **shallow** copy: the top level is independent, but…
Python Dictionary fromkeys() Method
`dict.fromkeys(keys, value=None)` is a class method that builds a new dictionary whose keys come from `keys`, all given the same …

Explore more

Python ProgramsPython ComparisonsPython How-To GuidesPython Cheat SheetsPython glossaryPython tutorial