How to Nest Settings Inside Other Settings in YAML
Settings that belong together go under one parent key in YAML, indented below it: a store's name and its address, and inside the address, the city and the postcode. The indentation is the structure, so it has to be even. This guide nests two levels deep and checks the result with Python.
Syntax
<parent>:
<key>: <value>
<child>:
<key>: <value>
<parent>:with nothing after the colon: the settings below belong to it.- The indentation: spaces at the start of the line. Every line in the same group has the same number of spaces; two per level is common.
<child>:: a group inside a group, indented further.
The YAML 1.2.2 specification says: "In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line."
The Example
store.yaml describes a bike shop: a store group with a name and an address group, which holds a city and a postcode.
Prerequisites
- uv and Python, with PyYAML in a project (
uv add pyyaml), to check the file. This guide uses PyYAML 6.0.4 on Python 3.15.2.
Step 1: Write the File
store:
name: Bike shop
address:
city: Leeds
postcode: LS1 1AA

Step 2: Check It with PyYAML
Save this checker as show.py. It loads any YAML file you give it and prints the result:
import sys
from pprint import pp
import yaml
with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
uv run python show.py store.yaml
{'store': {'name': 'Bike shop',
'address': {'city': 'Leeds', 'postcode': 'LS1 1AA'}}}

Each level of indentation became a dictionary inside a dictionary. In Python, the city is data["store"]["address"]["city"]. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | Example | What PyYAML did |
|---|---|---|
| Uneven indentation in one group | name indented by two spaces and city by three, both under store | mapping values are not allowed here, at line 3, column 8 |
| Indenting with a tab | A tab before name under store | found character '\t' that cannot start any token, at line 2, column 1 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
mapping values are not allowed here | A line is indented differently from the others in its group | Line up every key of the group with the same number of spaces |
| A key ends up in the wrong group | It's indented to a different level | Count the spaces and match the group it belongs to |
FAQ
How many spaces should I use?
Any number, as long as every key in a group uses the same: the same file indented by four spaces loaded the same way. Two spaces per level is the common choice.
What does the parent key's value hold?
The whole group below it: store holds a dictionary with name and address.
Conclusion
Indenting name and address under store, and city and postcode under address, gave dictionaries inside dictionaries. Keep each group's indentation identical, and use spaces only.
Additional Resources
- Indentation spaces — the specification's rules.
- PyYAML documentation — the library used to check the files.
