Dictionaries

In programming, data is only useful when you can organize and access it effectively. That’s where dictionaries come in. This lesson introduces dictionaries as one of the most flexible and powerful data structures you’ll encounter. Unlike lists, which use numeric positions, or dataclasses, which use predefined attributes, dictionaries allow you to map arbitrary keys to values. This concept mirrors a physical dictionary: given a word, you can find its definition. This powerful model enables fast lookups, dynamic data organization, and patterns that simplify many complex tasks.

Dictionary Basics

capitals = {
    "France": "Paris",
    "Japan": "Tokyo",
    "Brazil": "Brasília",
    "Canada": "Ottawa"
}

print(capitals["Canada"])
# Ottawa

In Python, dictionaries are written using curly braces {}, with each key-value pair separated by a colon (:) and each pair separated by commas. The keys and values can be of any type, but in practice they’re often strings or numbers. This lesson will show you how to create dictionary literals and assign dictionaries to variables so you can access and manipulate their data.

Using Dictionaries

inventory = {
    "sword": 1,
    "potion": 5,
    "gold": 42,
    "shield": 0
}

inventory['helmet'] = 7

del inventory['gold']

print(inventory)

Once you’ve created a dictionary, you can start using keys, update existing entries, and delete unwanted items. You’ll learn about dictionary access syntax, what happens when a key doesn’t exist (a KeyError is raised), and how to safely test whether a key exists using the in keyword. We’ll also look at how to iterate through dictionaries using .keys(), .values(), and .items() so you can loop through their contents. We’ll also look at how dictionaries are mutable, and how to visualize them in memory diagrams. Then we’ll introduces some common patterns of dictionary usage: using dictionaries as a replacement for if-else chains, for counting occurrences of items, and for grouping items into categories.

Lookup vs. Find

While dictionaries and lists are both ways to store collections of data, they serve very different purposes. Lists are great when you care about the order of items or want to access them by position. Dictionaries are great when you want to access items by arbitrary keys. They have tradeoffs in performance and flexibility depending on the operation you are using. However, some operations will always take time based on the size of the data structure (e.g. iterating through). You can also nest dictionaries and lists inside of each other to represent very complex data.

JSON

{
  "title": "Why JSON is Awesome",
  "author": {
    "name": "River",
    "id": 45
  },
  "tags": ["json", "web", "api"],
  "published": true,
  "views": 1345
}

To wrap up, this lesson explores how dictionaries work with dataclasses and JSON. You can easily convert between dictionaries and dataclasses, especially for representing structured, nested data. You can also map dictionaries directly onto JSON objects, which are used widely in web development and data exchange. By the end of this lesson, you’ll not only understand the fundamentals of dictionaries, but also how they fit into larger ecosystems of data management and communication.