Reading — step 1 of 5
Read
Block Sequences
A block sequence is a list with - markers per item.
fruits:
- apple
- banana
- cherry
Each item starts with - followed by the item value.
Items can be:
- Scalars: strings, numbers.
- Maps: indented after
-. - Lists: nested.
Map items:
people:
- name: Alice
age: 30
- name: Bob
age: 25
Each - starts a new map. The map's keys are at one indent more than the -.
Subtle: the name key's indent is BEYOND the - indent. The map under each - includes everything at that deeper indent.
Nested sequences:
matrix:
- - 1
- 2
- 3
- - 4
- 5
- 6
Yields [[1,2,3],[4,5,6]]. Each - opens a list within the parent list.
Mix of types in one list: allowed (heterogeneous).
mixed:
- 1
- hello
- true
- {key: value}
Empty list:
items: []
Or:
items:
Whether items: (no value) is empty list or null depends on parser; some treat as null.
Common operations:
- Iterating:
for item in cfg['items']. - Reading per-item maps:
cfg['people'][0]['name'].
Style choice: which indent for -?
# Option A: marker at parent indent
items:
- one
- two
# Option B: indent more
items:
- one
- two
Both valid YAML 1.2. Option B is more common in modern style guides.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…