Q&A
How to represent arrays in YAML
Overview
YAML ("YAML Ain't Markup Language") is a human-friendly data-serialization format widely used for configuration (Kubernetes, Docker Compose, CI pipelines, Ansible). An array in YAML terminology is a sequence: an ordered list of items. YAML offers two syntaxes that produce identical data.
graph TD
A[YAML Sequence / Array] --> B[Block style]
A --> C[Flow style]
B --> B1["- item one<br/>- item two<br/>- item three"]
C --> C1["[item one, item two, item three]"]
B & C --> D[Same parsed result:<br/>an ordered list]
style A fill:#1e1b4b,color:#fff
Block Style (most common)
Each element starts with a hyphen and a space, all at the same indentation level:
fruits:
- apple
- banana
- cherry
Parsed, fruits is a list of three strings. Indentation matters in YAML - items must align. Block style is preferred for readability in config files.
A top-level array (no key) is also valid:
- apple
- banana
- cherry
Flow Style (inline, JSON-like)
Compact lists fit on one line with brackets:
fruits: [apple, banana, cherry]
This is handy for short lists and is exactly how JSON would express it (YAML is a superset of JSON).
Arrays of Different Types
Array of numbers / mixed scalars
ports: [80, 443, 8080]
mixed:
- 42
- "forty-two"
- true
- null
Array of mappings (objects)
A very common pattern - a list where each item is a key/value object:
servers:
- name: web-1
ip: 10.0.0.1
role: frontend
- name: db-1
ip: 10.0.0.2
role: database
Here servers is a list of two maps. Note the indentation: the - introduces each item, and the keys of that item align under it.
Nested arrays (array of arrays)
matrix:
- [1, 2, 3]
- [4, 5, 6]
# or block style:
grid:
-
- a
- b
-
- c
- d
Empty array
tags: []
Real-World Example (Docker Compose style)
services:
web:
image: nginx:latest
ports:
- "80:80"
- "443:443"
environment:
- NODE_ENV=production
- DEBUG=false
depends_on: [db, cache]
This combines block-style sequences (ports, environment) and a flow-style sequence (depends_on) in one file - both are valid.
Common Gotchas
- Indentation must use spaces, never tabs. Tabs are forbidden in YAML and cause parse errors.
- Consistent indentation - all items in a sequence must align at the same column.
- Quoting - quote strings that contain special characters (
:,#, leading-) or that look like other types ("yes","3.14","true") when you want them treated as strings. - The hyphen needs a trailing space -
-appleis a scalar string,- appleis a list item. - Don't mix styles confusingly within one list (it's legal but harms readability).
Why This Matters in Interviews
DevOps and backend interviews often involve reading or writing YAML for Kubernetes, CI/CD, or Ansible. Know both array styles, how to express a list of objects (the most common real-world shape), and the tab/indentation rules that cause the majority of YAML errors.
Common Pitfalls
- Using tabs → instant parse failure.
- Forgetting the space after the hyphen.
- Misaligned indentation merging or breaking nesting.
- Unquoted values that YAML coerces to the wrong type (e.g.
nobecoming booleanfalse).
References
- YAML specification: https://yaml.org/spec/
- YAML on Wikipedia for a quick overview: https://en.wikipedia.org/wiki/YAML