How to Write a List in YAML
A list in YAML is a key with its items below it, one per line, each after a dash and a space. Python reads it as a list. This guide writes a shopping list, the same list on one line, and a list of groups, and checks each one with Python.
Syntax
<key>:
- <item>
- <item>
<key>: [<item>, <item>]
<key>:with nothing after the colon: the list below belongs to it.- <item>: a dash, a space, then the item. Each item gets its own line, and every dash sits in the same column.[<item>, <item>]: the same list on one line, between square brackets, with a comma between the items.
The YAML 1.2.2 specification calls a list a sequence: "A block sequence is simply a series of nodes, each denoted by a leading “-” indicator. The “-” indicator must be separated from the node by white space." For the one-line form, it says: "Flow sequence content is denoted by surrounding “[” and “]” characters."
The Example
shopping.yaml holds a shopping list: a fruit key with three items, apple, pear and plum. Later steps write the same kind of list in other ways.
Prerequisites
- uv and Python, with PyYAML in a project (
uv add pyyaml), to check the files. This guide uses PyYAML 6.0.4 on Python 3.15.2.
Step 1: Write the List
fruit:
- apple
- pear
- plum

Each item is indented by two spaces, so the dashes line up under each other.
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 shopping.yaml
{'fruit': ['apple', 'pear', 'plum']}

The three lines became a Python list, in the same order: ['apple', 'pear', 'plum']. In Python, the first item is data["fruit"][0]. The command works unchanged in PowerShell.
Step 3: Put the Dashes Under the Key
The dashes can also start in the same column as the key:
fruit:
- apple
- pear
- plum
uv run python show.py same-column.yaml
{'fruit': ['apple', 'pear', 'plum']}

The same list. The specification allows it because "people perceive the “-” indicator as indentation". Pick one style and keep it in a file.
Step 4: Write a Short List on One Line
vegetables: [carrot, leek, onion]
uv run python show.py flow.yaml
{'vegetables': ['carrot', 'leek', 'onion']}

The square brackets give the same kind of list. They suit short lists; long ones read better one item per line.
Step 5: Make a List of Groups
An item can be a group of keys. Write the first key after the dash, and line up the item's other keys under it:
orders:
- item: tea
units: 2
- item: coffee
units: 1

uv run python show.py orders.yaml
{'orders': [{'item': 'tea', 'units': 2}, {'item': 'coffee', 'units': 1}]}

Each dash started a new item, and each item became a dictionary: a list of two orders, each with an item and its units. units: 2 came back as the number 2, not the text '2'.
Common Mistakes
| Mistake | Example | What PyYAML did |
|---|---|---|
| No space after the dash | -apple and -pear on the lines under fruit: | No error: fruit became the text '-apple -pear', not a list |
| The first dash on the key's line | fruit: - apple | sequence entries are not allowed here, at line 1, column 8 |
| A dash indented further than the one above | - apple, then - pear with three spaces | No error: one item, 'apple - pear' |
The two mistakes without an error are the dangerous ones: the file loads, but holds something else. When a list comes back wrong, print it, as show.py does.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
sequence entries are not allowed here | A dash on the same line as its key, or where a key was expected | Put each item on its own line under the key |
The list is one text, like '-apple -pear' | No space after the dashes | Write - apple, with a space |
| Two items merged into one | One dash is indented differently from the others | Line up every dash in the same column |
FAQ
Can one list mix text, numbers and true or false?
Yes. A list with apple, 42 and true loaded as ['apple', 42, True]: each item keeps its own type.
How do I write an empty list?
With empty square brackets: fruit: [] loaded as {'fruit': []}.
Conclusion
fruit: followed by - apple, - pear and - plum loaded as the Python list ['apple', 'pear', 'plum'], with the dashes indented or in the key's column. [carrot, leek, onion] wrote a short list on one line, and items with keys made a list of dictionaries. A dash needs a space after it, its own line, and the same column as the others.
Additional Resources
- Block sequences: lists with dashes, in the specification.
- Flow sequences: lists between square brackets.
- PyYAML documentation: the library used to check the files.
