In this shot, 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.
A sequence is a linear data structure that represents a collection of elements. The elements in the sequence are accessed one after another.
-) 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 (
]). For example, the following is how the above list of fruits can also be represented:
fruits: ['apple', 'banana', 'orange']
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.
View all Courses