Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Array and Dictionary in YAML

Tags:

yaml

I understand the difference between an array and dictionary, say in python - especially when it comes to accessing content in both.

But in YAML, I kinda find it difficult to differentiate between the two especially when it comes to writing structures. Please How do they differ in their definitions?

like image 584
Brown Caveman-Hudson Mbong Avatar asked Oct 23 '25 20:10

Brown Caveman-Hudson Mbong


1 Answers

# YAML sequences (aka Python lists)

# option 1: block style
departments:
  - marketing
  - sales
  - security

# option 2: flow style

departments: [marketing, sales, security]


# YAML dictionary

marketing:
  team-size: 20
  location: nyc

# expanding the sequence to include dictionaries

departments:
  - marketing:
      team-size: 20
      location: nyc
  - sales:
      team-size: 30
      location: sf
  - security:
      team-size: 10
      location: mia

Given your familiarity with Python, I'll focus more on syntax:

  • Sequences come in two styles: block and flow. When using block style, each element in the list is preceded by a dash and space -
  • Dictionaries come in the form of key: value pairs. They are defined with a name, colon and space name:
  • For every key: value pairing, the value is stored as a scalar (i.e. single value, not real number)
  • These scalars themselves can become keys to other value mappings
  • You can have nested sequences (lists) and nested mappings (dictionaries)

To clarify with an example: option 1 above shows a block style sequence. This is denoted with the spacing + -. We can convert each element, or value, in that mapping to a scalar by creating further indentations without the - (see #expanding the sequence)

like image 158
wls2121 Avatar answered Oct 27 '25 05:10

wls2121



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!