0Pricing
Helm Academy · Lesson

Looping Over Lists and Maps with range

Generating repeated YAML from a collection.

Looping Over Lists and Maps with range is a free Helm Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Helm Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Repeat Without Copy-Paste

When a value holds a list, you rarely want to type each entry by hand. The range action loops over a collection and renders YAML for every item.

The range Action

A loop opens with {{ range SOMETHING }} and closes with {{ end }}. The block between them renders once per element in the collection. 🔁

{{ range .Values.hosts }}
- host: {{ . }}
{{ end }}

The Dot Becomes the Item

Inside a range, the dot is rebound to the current element, not the root. So {{ . }} prints whatever item this pass is on.

hosts:
  - api.example.com
  - web.example.com

Looping a Simple List

Given a list of strings, range emits one YAML line per value. The result is a clean, dynamically sized list driven entirely by values.

args:
{{ range .Values.flags }}
  - {{ . }}
{{ end }}

Index and Value

Need the position too? Capture both with two variables. Range hands you the index first and the value second on each pass.

{{ range $i, $v := .Values.ports }}
# port {{ $i }} is {{ $v }}
{{ end }}

Ranging Over a Map

Range a map and the two variables become the key and the value. This is perfect for turning a values map into labels or env vars.

{{ range $key, $val := .Values.labels }}
{{ $key }}: {{ $val }}
{{ end }}

Map Keys Come Sorted

Helm iterates map keys in alphabetical order, not insertion order. That keeps your rendered output stable and diff-friendly across runs.

Reaching the Root Inside

Since the dot is now the item, .Values is out of reach inside the loop. Capture the root first with $ so you can still read it.

{{ range .Values.envs }}
name: {{ $.Release.Name }}-{{ . }}
{{ end }}

Empty Lists Render Nothing

If the collection is empty, range simply produces no output and skips its block. No items means no loop body, which is exactly what you want.

Watch Your Indentation

Each pass must align under the parent key. Loops often pair with the indent helper so the repeated YAML lands at the right depth. 🧱

Combine range With if

Nest an if inside a range to filter as you loop. Only the items that pass the condition render their block, skipping the rest.

{{ range .Values.users }}
{{ if .active }}
- {{ .name }}
{{ end }}
{{ end }}

Quick Check

You loop with {{ range .Values.hosts }} over a list of strings.

Recap

You can now loop with range, read items via the rebound dot, grab index or key, and reach the root with $. Repetition tamed! 🎯

Frequently asked questions

Is the “Looping Over Lists and Maps with range” lesson free?

Yes — the full text of “Looping Over Lists and Maps with range” is free to read here on the web, and the Helm Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Helm Academy course, upgrade to CoddyKit PRO.

What will I learn in “Looping Over Lists and Maps with range”?

Generating repeated YAML from a collection. You practise Helm Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Helm Academy?

No prior experience is required. Helm Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping Over Lists and Maps with range” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Helm Academy lesson?

Yes. Every Helm Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Conditional Blocks with if and else
  2. Truthiness and Operators in Conditions
  3. Looping Over Lists and Maps with range
  4. Scoping a Block with with
← Back to Helm Academy