How to represent sequence in YAML?

In this Answer, we'll learn about sequence and how to represent it in YAML. We'll also see how to use the different styles to represent a sequence and a nested sequence.

What is sequence?

A sequence is a linear data structure that represents a collection of elements. The elements in the sequence are accessed one after another.

YAMLYAML is a human-readable data serialization language. It is used to represent structured data. YAML files usually have the .yaml or .yml extension. provides various ways to represent sequence structures. The most basic form of representing a sequence is using a dash (-) followed by space and then specifying the element. For example, the following is how a list of fruits can be represented:

- apple
- banana
- orange

Another way to represent a sequence is by using the start and end indicators ([ and ]). For example, the following is how the above list of fruits can also be represented:

fruits: ['apple', 'banana', 'orange']

Nested sequence

A nested sequence is a sequence that is contained within another sequence. We can also use nested sequences to represent more complex data structures.

For example, the following is how a list of people with their hobbies can be represented:

-
name: John
hobbies: ['sports', 'music']
-
name: Jane
hobbies: ['movies', 'cooking']
-
name: Mark
hobbies: ['tennis', 'swimming']

In the above example, each person has a list of hobbies. This is represented using a nested sequence.

Note: If you want to learn more about the use of YAML to represent key-value pairs and other data structures, you can check out the Introduction to YAML course for more information.

Unlock your potential: YAML basics series, all in one place!

To continue your exploration of YAML basics, check out our series of Answers below:

Free Resources