---
title: "Traversing a List to Build a Result"
description: "Looping over every item in a collection while carrying a running result: a total, a count, or the largest value seen so far."
canonical: https://lightmysky.com/learn/computing/traversing-a-list-to-build-a-result-mt_lusbjHFt95
source: https://lightmysky.com/learn/computing/traversing-a-list-to-build-a-result-mt_lusbjHFt95.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Traversing a List to Build a Result

Looping over every item in a collection while carrying a running result: a total, a count, or the largest value seen so far.

Subject: Computing · Area: Programming · Ages 15 to 16
Page: https://lightmysky.com/learn/computing/traversing-a-list-to-build-a-result-mt_lusbjHFt95

## Ready when they can

- Total the numbers in a list with a loop and a running variable
- Count how many items match a condition
- Find the largest value without a built-in function, and justify the starting value

## Lesson: One pass, one running answer

To total a list you walk it once and carry a running total. Start the total at 0, then add each item in turn. For 4, 7, 2 and 9 the total grows 4, 11, 13 and lands on 22.

**Example.** Counting matches works the same way with a counter starting at 0. In 3, 8, 15, 22, 6 and 19, only 15, 22 and 19 pass the over-10 test, so the counter lands on 3.

To find the largest, carry the best seen so far and start it at the first item. In 12, 45, 7, 39, 45 and 3 the best ends at 45. Starting at 0 would lie on an all-negative list like -8, -2, -15, -4, since 0 beats them all yet sits in no slot.

**Tip.** Off-by-one slips lurk at the ends, so always test the first item, the last item and the empty list.

**Recap.** Walk every item once while a running total, counter or best-so-far carries your answer forward.

## Practice

18 questions on this page, each with its working shown.

## Needs first

- [Lists: Many Values Under One Name](https://lightmysky.com/learn/computing/lists-many-values-under-one-name-mt_ipB6kmyK85)

## Opens up

- [Linear Search](https://lightmysky.com/learn/computing/linear-search-mt_32LwZIhSiB)
- [Quadratic Sorts: Bubble and Insertion](https://lightmysky.com/learn/computing/quadratic-sorts-bubble-and-insertion-mt_cBUIBa15jQ)
- [Defining a Function with Parameters](https://lightmysky.com/learn/computing/defining-a-function-with-parameters-mt_pyo7yBg4H3)
