A YAML array is a sequence, and you write it one of two ways. Both parse to exactly the same list:
colors: colors: [red, green]
- red
- green
both -> {'colors': ['red', 'green']}
Block style (the dashes) for anything you will read again; flow style (the brackets) for short lists and for nesting inside another structure. Every result on this page is repr() output from PyYAML 6.0.2, re-checked against Ruby’s Psych, which wraps the C libyaml and shares no code with PyYAML.
Table of Contents
The indentation rule that trips everyone
A block sequence under a key does not have to be indented past the key. These two documents are the same document:
colors: colors:
- red - red
- green - green
both -> {'colors': ['red', 'green']}
Both parsers returned the identical list for both forms. The dash itself is the indentation, so a sequence is allowed to sit flush with its parent key. Kubernetes manifests usually indent; Ansible usually does not; neither is more correct and mixing them in one file is legal. Knowing this saves you from “fixing” indentation that was never broken.
Where indentation genuinely matters is inside a sequence of mappings. The keys after the first one must line up with the first key, not with the dash:
employees:
- id: 1
name: John
- id: 2
name: Jane
-> {'employees': [{'id': 1, 'name': 'John'}, {'id': 2, 'name': 'Jane'}]}
Move name left to align with the dash instead and PyYAML raises yaml.parser.ParserError: while parsing a block collection. That is the one array mistake that fails loudly. The next two do not.
[] and an empty key are not the same thing
This is the real source of bugs, and the earlier version of this page got it exactly backwards. It said: “Empty Array: Use square brackets [] or simply omit the elements.” Omitting the elements does not give you an empty array. It gives you null:
colors: [] -> {'colors': []} Python list, length 0
colors: -> {'colors': None} Python None
colors: null -> {'colors': None}
colors: ~ -> {'colors': None}
Ruby returns [] and nil for the same two documents. The consequence, run rather than described:
doc 'colors: []' value [] -> iterated 0 items
doc 'colors:' value None -> TypeError: 'NoneType' object is not iterable
In Ruby the second one is NoMethodError: undefined method `each' for nil:NilClass. A config template that leaves a key with no value ships a crash, not an empty list. If a downstream loop must always run, write [].
A missing space after the dash is silent
The old page listed “omitting the dash (-) before array items” under errors “which can lead to syntax errors”. There is no syntax error. Write the dash without the space after it and the whole block collapses into one string:
colors:
-red
-green
-> {'colors': '-red -green'}
Both parsers agree, and both return a string. Nothing warns you; the document is valid YAML, just not the document you wrote. A lone - with nothing after it is also valid and produces a null element: ['red', None, 'green'].
You do not need to convert a JSON array
The old FAQ said to convert JSON to YAML by “replac[ing] square brackets with dashes for each element”. Skip that — flow style is JSON syntax, so a JSON document is already a YAML document:
{"colors": ["red", "green"], "port": 8080}
json.loads -> {'colors': ['red', 'green'], 'port': 8080}
yaml.safe_load -> {'colors': ['red', 'green'], 'port': 8080}
equal: True
RFC 9512 §3.4 puts the same point from the other direction: “When using flow collection styles (see Section 7.4 of [YAML]), a YAML document could look like JSON [JSON]”.
Nesting, and the types you did not ask for
teams: m:
- name: A - - 1
members: - 2
- Alice - - 3
- Bob
{'teams': [{'name': 'A', {'m': [[1, 2], [3]]}
'members': ['Alice', 'Bob']}]}
Elements are typed the same way any other scalar is, which is where the surprises live:
k: [yes, no, 8080, '8080'] -> {'k': [True, False, 8080, '8080']}
Two of those four items are booleans. That is not an array rule, it is YAML’s implicit typing, and it has its own set of traps — see YAML syntax and type coercion rather than a summary here. Two more things this page deliberately does not re-explain: block scalars for multiline values, and where # is and is not a comment.
Two smaller facts, both verified: a flow sequence may span lines and may carry a trailing comma ([red, green,] parses fine), and a value containing a comma must be quoted inside flow style — ['a, b', c] gives two elements, [a, b, c] gives three.
Not tested here: Go’s gopkg.in/yaml.v3, js-yaml, or snakeyaml. Everything above ran on PyYAML 6.0.2 and Psych 3.1.0 only, and the accompanying verify script re-parses every example and asserts the exact object, so you can falsify the page in one run.
Check this yourself. Every command and every block of output on this page is reproduced by /verify/how-to-create-yaml-array.sh. Download it and run it: it creates its own scratch files, prints one line per claim, cleans up after itself, and exits non-zero if any claim here turns out to be wrong. If it disagrees with this page, the page is wrong.