Dictionaries are a common data structure in many programming languages. A dictionary is a data structure that allows us to store data in key-value pairs.
In YAML, dictionaries are represented as mappings. A mapping is a collection of key-value pairs, where each key is mapped to a value.
For example, the following is a graphical representation of a dictionary that maps several countries to their capital cities.
In the example above, the countries mapping contains five key-value pairs. The keys are on the left, and the values are on the right. The keys are "Afghanistan", "Albania", "Algeria", "Andorra", and "Angola," and their corresponding values are "Kabul", "Tirana", "Algiers", "Andorra la Vella", and "Luanda," respectively.
When we want to represent a dictionary in YAML, we can use either block-style or flow-style notation.
The following is YAML code for the above mapping represented in block-style notation:
---countries:Afghanistan: KabulAlbania: TiranaAlgeria: AlgiersAndorra: Andorra la Vella"Angola": "Luanda"
As we can see, in block-style notation each key is separated from its value by a colon (:
) followed by a space. We can use also quotation marks around the key or value strings, as in line 7 above.
We can also represent mappings in flow-style notation. The following is an equivalent YAML code to represent the mapping above in flow-style notation:
countries: {Afghanistan: Kabul, Albania: Tirana, Algeria: Algiers, Andorra: Andorra la Vella, "Angola": "Luanda"}
In flow-style notation, all of the key-value pairs are represented on a single line. As we can see, the dictionary is represented as a sequence of key-value pairs. We use the mapping start token ({
) and the mapping end token (}
) to map the key-value pairs in flow-style representation. These are used to indicate the beginning and the end of a mapping, respectively.
Each key is separated from its value by a colon (:
) followed by a space. The key-value pairs are separated by commas (,
).
Flow-style notation is more compact than block-style notation. However, it can be more difficult to read. Therefore, it is generally advisable to use the block-style notation for mappings that contain more than a few key-value pairs.
Unlock your potential: YAML basics series, all in one place!
To continue your exploration of YAML basics, check out our series of Answers below:
What is a YAML file?
Understand what YAML files are and the different styles they use.
What is block style in YAML?
Learn the block style format for structuring data in YAML.
What is flow style in YAML?
Discover how flow style differs from block style and when to use it.
How to represent different basic data types in YAML
Explore how YAML handles various basic data types, such as strings, integers, and booleans.
How to represent strings in YAML
Discover how YAML handles string values and different formatting options.
How to represent arrays in YAML
Explore how YAML represents arrays, with each element preceded by a hyphen (-
).
How to represent key-value pairs in YAML
Learn how to effectively represent key-value pairs within YAML syntax.
How to represent maps in YAML
Master how to structure key-value pairs as maps in YAML.
How to represent sequence in YAML?
Understand how to represent sequences in YAML files.
How to represent dictionaries in YAML
Explore how to represent complex data structures like dictionaries in YAML.
How to represent null values in YAML
Learn how to properly represent null or missing values in YAML.
How to write comments in YAML
Learn how to add comments in YAML to improve readability.
What are the advantages of using YAML over other data formats?
Understand why YAML is often preferred over JSON and XML for data serialization.