CodingNic

Python Dictionaries

Dictionary Basics

Python Dictionaries 20 min read

Dictionary Basics

Objectives

By the end of this chapter, you should be able to:

  • Create a Python dictionary
  • Access and modify values in a dictionary
  • Check whether a key exists in a dictionary
  • Use common dictionary methods

💡 Why this matters: Most real data is labeled, not just ordered. A dictionary is how Python represents that, and you’ll reach for one constantly alongside lists.

What Is a Dictionary?

A dictionary stores key-value pairs. You access a value by passing its key in square brackets. One way to build one is with curly braces, a colon between each key and value, and commas between pairs:

python
book_authors = {
    "The Long Horizon": "Erin",
    "Paper Moons": "Jordan",
    "The Salt Garden": "Maya"
}
book_authors["The Long Horizon"] == "Erin"  # True

You can also build one with the dict function, assigning values to keys with =:

python
another_dictionary = dict(key="value")
another_dictionary  # {'key': 'value'}
another_dictionary["key"] == "value"  # True
another_dictionary["another_key"]  # KeyError: that key doesn't exist

Reassigning and Adding Keys

= both reassigns an existing key and creates a brand-new one:

python
another_dictionary = dict(key="value")
another_dictionary["key"] = "new value"
another_dictionary["another_key"] = "another value"
another_dictionary  # {'key': 'new value', 'another_key': 'another value'}

Built-In Dictionary Methods

Method Does
.clear() Removes every key-value pair
.copy() Returns a new, independent dictionary with the same pairs
.fromkeys(keys, value) Builds a new dictionary, assigning value to every key given
.get(key) Returns the value for key, or None instead of raising an error if it’s missing
.items() Returns every key-value pair, as (key, value) tuples
.keys() Returns every key
.pop(key) Removes key and returns its value; raises KeyError if missing (unlike list’s .pop(), a key is required)
.popitem() Removes and returns the most recently added key-value pair
.update(other) Merges another dictionary in, overwriting any matching keys

A couple worth a closer look. .get() is the safe alternative to bracket access when a key might not exist:

python
d = dict(a=1, b=2, c=3)
d["no_key"]      # KeyError
d.get("no_key")  # None (no error)

.update() overwrites matching keys rather than merging their values:

python
first = dict(a=1, b=2, c=3)
second = {}
second.update(first)
second["a"] = "changed"
second.update(first)
second  # {'a': 1, 'b': 2, 'c': 3} (update() overwrote "a" back)

Try It

  1. Build a dictionary of three or four key-value pairs and compare .get() against bracket access on a key that doesn’t exist.
  2. Use .update() to merge two dictionaries that share a key, and predict which value wins before you run it.
  3. Use .pop() to remove one key-value pair, then confirm the dictionary’s contents afterward.

Recap

  • Dictionaries store key-value pairs; access with [] (raises KeyError if missing) or .get() (returns None instead).
  • .pop(key) removes and returns a value by key; .popitem() removes the most recently added pair.
  • .update() merges another dictionary in, overwriting any matching keys.

Next lesson: looping over dictionaries, dictionary comprehension, and two more collection types: tuples and sets.