Skip to main content

How to Write a List in YAML

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<key>:
- <item>
- <item>
Syntax (on one line)
<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​

shopping.yaml
fruit:
- apple
- pear
- plum

shopping.yaml in a macOS-style code window: the key fruit, then three lines indented by two spaces, - apple, - pear and - 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:

show.py
import sys
from pprint import pp

import yaml

with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
Terminal
uv run python show.py shopping.yaml
Output
{'fruit': ['apple', 'pear', 'plum']}

The terminal after uv run python show.py shopping.yaml: a dictionary whose fruit key holds the list 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:

same-column.yaml
fruit:
- apple
- pear
- plum
Terminal
uv run python show.py same-column.yaml
Output
{'fruit': ['apple', 'pear', 'plum']}

The terminal after uv run python show.py same-column.yaml: the same fruit list, 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​

flow.yaml
vegetables: [carrot, leek, onion]
Terminal
uv run python show.py flow.yaml
Output
{'vegetables': ['carrot', 'leek', 'onion']}

The terminal after uv run python show.py flow.yaml: the vegetables key holds the list 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.yaml
orders:
- item: tea
units: 2
- item: coffee
units: 1

orders.yaml in a macOS-style code window: the key orders, then two items, each a dash followed by item and units, with units lined up under item.

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

The terminal after uv run python show.py orders.yaml: orders holds a list of two dictionaries, item tea with units 2 and item coffee with 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​

MistakeExampleWhat 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 linefruit: - applesequence entries are not allowed here, at line 1, column 8
A dash indented further than the one above - apple, then - pear with three spacesNo 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​

SymptomLikely causeFix
sequence entries are not allowed hereA dash on the same line as its key, or where a key was expectedPut each item on its own line under the key
The list is one text, like '-apple -pear'No space after the dashesWrite - apple, with a space
Two items merged into oneOne dash is indented differently from the othersLine 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​