A list in YAML is a key followed by its items, one per line, each starting with a dash and a space. The items keep the order you write them in.
Every command and output on this page is real, run with yq 4.54.3 on macOS.
Syntax
key:
- item
- item
key:: the name of the list, with nothing after the colon.-: a dash and a space start each item.item: the item’s value. All the items line up at the same indent.
See Block Sequences (opens in a new tab) in the YAML 1.2.2 specification.
Prerequisites
- yq, to check the YAML. Install it from yq’s install instructions (opens in a new tab).
The Example
A basket of three fruits, in a file named basket.yml.
Step 1: Write the YAML
fruits:
- apple
- banana
- mango
Step 2: Check It with yq
yq reads the file and prints it back:
yq '.' basket.yml
fruits:
- apple
- banana
- mango
Step 3: Read One Item
yq '.fruits[1]' basket.yml
banana
[1] picks the second item: yq counts list items from 0.
Step 4: Count the Items
yq '.fruits | length' basket.yml
3
length counts the items in the list.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No space after the dash: -apple | No error, but the list is gone: yq '.' basket.yml prints fruits: -apple - banana - mango, one line of text | Put a space after every dash |
FAQ
Why does .fruits[1] print banana, not apple?
yq counts list items from 0, so apple is [0] and banana is [1].
Can a list hold numbers?
Yes. An item can be any value, such as text, a number or true, and each item is written after its own dash.
Conclusion
Write the key with nothing after its colon, then one item per line, each after a dash and a space, all at the same indent.
Additional Resources
- Block Sequences (opens in a new tab) (YAML 1.2.2 specification)
- Traverse (Read) (opens in a new tab) (yq documentation)
- Length (opens in a new tab) (yq documentation)
0 comments