How to represent arrays in YAML

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.

%0 node_1 red node_2 green node_3 blue node_1627442199807 organge
Array or list of colors

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

colors: 
  - red
  - green
  - blue
  - orange

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

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.

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.

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!

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

Question 1 of 20 attempted

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

Haven’t found what you were looking for? Contact Us


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"

Free Resources