How to represent arrays in YAML: 2026 guide

How to represent arrays in YAML: 2026 guide

Learn how to represent arrays in YAML using block and flow sequences with clear examples. Explore nested arrays, arrays of objects, JSON equivalents, and real-world configuration patterns used in GitHub Actions, Docker Compose, and Kubernetes.

3 mins read
Aug 10, 2026
Share

Key takeaways:

  • Arrays are fundamental data structures used to store a collection of values.

  • YAML provides two styles for representing arrays: block sequence and flow sequence.

  • Block sequence uses hyphens - to list array elements, while flow sequence uses square brackets [] and commas.

  • The choice between block or flow sequence in YAML depends on readability and context.

Arrays#

Arrays are one of the most fundamental data structures in programming. They are used to store multiple values in a single variable and are available in almost all programming languages, including Perl, Python, PHP, Java, C#, Ruby, and JavaScript. In simple terms, an array is a list of items that allows us to group data together and access each item by its index.

widget


Why arrays matter?

Arrays are essential for efficiently handling collections of data. They allow us to:

  • Group data: Arrays are ideal for holding collections of similar items (e.g., a list of colors, names, or numbers).

  • Access items quickly: Arrays provide quick access to individual elements via indexing.

  • Manipulate data easily: Operations such as sorting, filtering, and iterating over arrays are straightforward.

Arrays in YAML: Block vs. flow sequences#

YAML is a human-readable data serialization format often used for configuration files, data storage, and communication between different systems. YAML supports arrays in two main styles:

Block sequence#

The block sequence style of YAML uses hyphens or dashes to (-) to represent arrays. A hyphen (-) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level.See the code example below to understand this better.

Example#

widget

Flow sequence#

The flow sequence style of YAML uses brackets and commas to represent arrays.

See the code example below to understand this better.

Example#

YAML
colors: [red, green, blue, orange]

Both the block and flow sequence styles are equivalent, but the choice between them often depends on the context and personal preference.

Arrays of objects in YAML#

In many configuration files, an array doesn't simply contain strings or numbers. Instead, each array element is an object (called a mapping in YAML) with multiple key-value pairs. This structure is widely used in Kubernetes manifests, Docker Compose files, GitHub Actions workflows, and Ansible playbooks.

For example, the following YAML defines an array of users:

YAML
users:
- id: 1
name: Alice
role: Admin
- id: 2
name: Bob
role: Developer
- id: 3
name: Charlie
role: Tester

The same structure in JSON looks like this:

Node.js
{
"users": [
{ "id": 1, "name": "Alice", "role": "Admin" },
{ "id": 2, "name": "Bob", "role": "Developer" },
{ "id": 3, "name": "Charlie", "role": "Tester" }
]
}

Arrays of objects make YAML easy to read while still representing structured data.

Nested arrays in YAML#

YAML also supports arrays inside other arrays, often referred to as nested or multi-dimensional arrays. These are useful when representing grouped values or hierarchical data.

Here's an example:

YAML
matrix:
-
- apple
- banana
-
- orange
- grape

Nested arrays also appear in CI/CD pipelines, matrix testing, and deployment configurations where multiple combinations of operating systems, languages, or environments need to be defined.

Because indentation determines structure in YAML, each nested array must remain consistently aligned.

Creating arrays in YAML using Python#

In Python, a YAML array is a list. An array (or list) is an ordered collection of items, which can be of any type. In YAML, an array is typically represented with hyphens (-), and the values can be strings, integers, or other data types.

Python
import yaml
# Load YAML data with block sequecne
arr1 = yaml.safe_load("""
- value1
- value2
- value3
""")
# Load YAML data with flow sequecne
arr2 = yaml.safe_load("""
[ red, green, blue, orange ]
""")
# Print the entire array
print(arr1)
print(arr2)

Want to learn more? Visit the Introduction to YAML course to master YAML syntax, data types, sequences, mappings, and more!

Real-world examples of YAML arrays#

You'll encounter YAML arrays in many modern development tools and infrastructure platforms.

GitHub Actions#

Arrays are commonly used to define workflow steps.

YAML
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test

Docker Compose#

Arrays define exposed ports.

YAML
ports:
- "3000:3000"
- "8080:8080"

Kubernetes#

Arrays are frequently used for containers within a pod specification.

YAML
containers:
- name: web
image: nginx
- name: api
image: node:20

Learning these common patterns makes it much easier to read and write YAML files used in real software projects.

Technical Quiz
1.

Given the following YAML code, how many items are in the colors array?

colors:
  - red
  - green
  - blue
  - orange
A.

5

B.

3

C.

4

D.

6


1 / 1

Best practices for YAML arrays#

YAML is designed to be human-readable, but small formatting mistakes can make a configuration file invalid. Following a few best practices helps avoid common errors.

  • Use spaces instead of tabs. YAML does not allow tabs for indentation.

  • Keep indentation consistent. Every item in a sequence should align at the same indentation level.

  • Use block sequences for long lists. Hyphen-based arrays are easier to read and maintain than inline arrays.

  • Use flow sequences for short values. Inline arrays ([red, green, blue]) work well for compact lists.

  • Quote ambiguous values such as yes, no, on, off, dates, or numbers when you want them treated as strings.

  • Represent empty arrays explicitly using [] when a configuration requires a list but no values are currently defined.

Following these conventions improves readability and helps prevent YAML parsing errors.

Conclusion#

Arrays are essential for organizing and manipulating data in many programming languages, and understanding how to represent them in different formats like YAML is a valuable skill for developers. Whether you're using arrays in programming languages like Python or JavaScript, or serializing data in YAML for configuration files, understanding how to structure and work with arrays is crucial.


Frequently Asked Questions

How can we represent maps in YAML?

We can represent maps in YAML in the following way:

map:
  - key1: value1
  - key2: value2
  - key3: value3

How can we represent strings in YAML?

We can represent strings in YAML in the following way:

# single-quoted scalars
'hello world'

# double-quoted scalars
"hello world"

How do you write an inline array in YAML?

An inline array uses flow sequence syntax, where values are enclosed in square brackets and separated by commas. This format is useful for short arrays, while block sequences are generally preferred for longer or more complex lists.

Can a YAML array contain different data types?

Yes. A YAML array can contain strings, numbers, booleans, null values, objects, and even nested arrays within the same sequence.

What’s the difference between block and flow sequences in YAML?

Both represent arrays and produce the same data structure.

  • Block sequences use hyphens (-) and are easier to read for long or complex lists.
  • Flow sequences use square brackets ([]) and commas, making them useful for short arrays that fit on a single line.

Choose the style that provides the best readability for your configuration file.


Written By:
Areeba Haider