YAML by Example: Sequences
1.2
Lists and arrays. Learn block and flow sequence syntax.
Code
# Block style (using dashes)
fruits:
- Apple
- Banana
- Orange
# Flow style (JSON-like)
vegetables: [Carrot, Potato, Onion]
# Nested sequences
matrix:
- [1, 2, 3]
- [4, 5, 6]
# List of objects
users:
- name: Alice
id: 1
- name: Bob
id: 2Explanation
Sequences in YAML represent lists or arrays. Block style uses a hyphen and space for each item on a new line. Flow style uses square brackets with comma-separated items, similar to JSON arrays. Both styles create the same data structure.
Sequences can contain any type of data including scalars, other sequences, or mappings. A list of mappings is common for collections of objects, like a list of users or configuration sets.
Code Breakdown
3
- Apple defines a list item. The dash must be followed by a space.8
[Carrot, Potato] is the inline flow syntax.
