TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 11 of 22 · By Ethan Cole · Updated November 21, 2026

Python Dictionaries: Keys, Values and Lookups

A dictionary maps keys to values. Instead of asking "what is at position 2?", you ask "what is the price?". Dictionaries are written with braces and colons: {"title": "Paper Cranes", "year": 2016}. Looking up a key is fast no matter how large the dictionary grows, which makes dicts the workhorse for counting, grouping and configuration.

Reading and writing keys

book["title"] returns the value for a key. If the key is missing, square-bracket lookup raises KeyError. When a key may be absent, use book.get("isbn"), which returns None, or book.get("isbn", "n/a") to supply your own default.

Assigning to a key adds it if it is new and replaces the value if it already exists. Keys are unique, so a dict never holds two entries for the same key. Since Python 3.7, dictionaries keep keys in the order they were first inserted; updating a value does not move its key.

Python 3 · editable

Expected output

{'title': 'Paper Cranes', 'year': 2017, 'price': 11.0}
Paper Cranes None n/a
True 3
['title', 'year', 'price']
('title', 'Paper Cranes')
11.0 {'title': 'Paper Cranes', 'year': 2017}
{'pens': 5, 'ink': 2}

Useful methods

  • keys(), values() and items() return live views of the dictionary. Wrap them in list() to get a snapshot, or loop over them directly (you will do that in the loops chapter).
  • pop(key) removes a key and returns its value.
  • update(other) copies every key from another dict, overwriting existing ones.
  • setdefault(key, default) returns the value, inserting the default first if the key is missing.
  • key in d checks keys, not values.

What can be a key?

Keys must be hashable, which in practice means immutable: strings, numbers, and tuples of those are fine. Lists and other dicts cannot be keys. Values have no restriction at all; a value can be a list, another dict, or anything else, which is how nested data such as JSON is represented in Python.

Common mistakes

  • Using d[key] for optional keys and crashing with KeyError. Prefer get().
  • Expecting "Paper Cranes" in book to search values. It checks keys; use in book.values().
  • Changing a dict's size while looping over it. Loop over list(d) if you need to delete keys.

Summary

  • Dicts map unique, hashable keys to values.
  • d[k] raises on a missing key; d.get(k, default) does not.
  • Assignment adds or replaces; pop removes; update merges.
  • Insertion order is preserved.

Quiz: Dictionaries

1. What does {"a": 1}.get("b", 0) return?

get returns the supplied default when the key is missing.

2. What is len({"x": 1, "x": 2})?

Keys are unique; the second "x" replaces the first value.

3. Which type cannot be used as a dictionary key?

Keys must be hashable; lists are mutable and unhashable.

4. What does "Paper Cranes" in {"title": "Paper Cranes"} return?

in checks the keys of a dictionary, and the only key is "title".

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.