We can represent maps in YAML in the following way:
map:
- key1: value1
- key2: value2
- key3: value3
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 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.
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.
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:
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.
colors:
- red
- green
- blue
- orange
The flow sequence style of YAML uses brackets and commas to represent arrays.
See the code example below to understand this better.
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.
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 sequecnearr1 = yaml.safe_load("""- value1- value2- value3""")# Load YAML data with flow sequecnearr2 = yaml.safe_load("""[ red, green, blue, orange ]""")# Print the entire arrayprint(arr1)print(arr2)
Want to learn more? Visit the Introduction to YAML course to master YAML syntax, data types, sequences, mappings, and more!
Given the following YAML code, how many items are in the colors array?
colors:
- red
- green
- blue
- orange
5
3
4
6
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.
Haven’t found what you were looking for? Contact Us