LESSON · 1 OF 5

Creating dictionaries

Creating dictionaries

A dictionary maps keys to values. Where a list finds items by numeric position, a dictionary finds them by a name you choose. Write one with curly braces and key: value pairs:

python
config = {"app": "billing", "port": 8080, "debug": False}

Each pair joins a key on the left to a value on the right. Here the key "app" maps to the value "billing", and "port" maps to 8080.

Why dictionaries fit DevOps work

Configuration, JSON payloads, API responses, and environment settings are all key-to-value data. A dictionary is the shape that data takes in Python, so you'll meet dictionaries constantly. This structure reads much better than trying to remember that the port lives at index 1 of some list.

Keys and values

Values can be anything: strings, numbers, booleans, even lists or other dictionaries. Keys are almost always strings in the config and JSON data you'll handle:

python
server = {
    "hostname": "web-01",
    "ports": [80, 443],
    "healthy": True,
}

Splitting a dictionary across lines like this, one pair per line, is the usual style once it grows past two or three keys. The trailing comma after the last pair is allowed and makes edits cleaner.

Keys are unique

Each key appears once. If you write the same key twice, the last value wins:

python
d = {"port": 80, "port": 8080}
print(d)   # {'port': 8080}

The empty dictionary

An empty dictionary is a pair of braces. You'll often start with one and fill it in as your program runs:

python
settings = {}
Spin up a fresh environment and practice live.
python-devops · fresh machine · ready in under a minute