✦ Python Foundations Collections module

Concept 12 · Iterating over Collections

Visit every value.
Let a loop do the work.

A for loop visits a collection one item at a time. Name the current item, run the indented body, then continue with the next item.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A for loop visits one collection item at a time.

You already know for loops. Here you use the same mechanism with collections: collection → for loop → one item per iteration → loop body. Python assigns the current item to the loop variable, runs the indented body, and then moves to the next item.

names = ["Ana", "Marko", "Jelena"]

for name in names:
    print(name)

Python takes "Ana", assigns it to name, and runs print(name). It repeats with "Marko" and "Jelena", then stops. name is one current value during one iteration; it is not the whole names list. The same pattern works with a tuple: for coordinate in (45.26, 19.83) visits each coordinate in order.

Ana Marko Jelena

Trace the loop explicitly: iteration 1 → color = "red"; iteration 2 → color = "blue"; iteration 3 → color = "green"; then the loop ends. Each turn assigns a new current value before the indented body runs.

↳

A loop can build a result too: With numbers = [2, 4, 6] and total = 0, each total += number builds the result: 0 → 2 → 6 → 12. A loop does not only print items; it can accumulate a value across iterations.

Dictionary iteration starts with keys
student = {"name": "Ana", "course": "Python", "city": "Novi Sad"}

for key in student:
    print(key)

A plain for key in student loop visits dictionary keys, not values. Use student.values() when values are the useful thing to visit. Use student.items() when you need both parts.

Choose values or key-value pairs
for value in student.values():
    print(value)

for key, value in student.items():
    print(key, value)

student.values() visits values. student.items() conceptually provides pairs such as ("name", "Ana"). Because each pair has two parts, Python can unpack it into key, value—just like the tuple unpacking you already know.

A set visits every member, without a reliable position
tags = {"python", "django", "ai"}

for tag in tags:
    print(tag)

Every set member is visited, but do not build logic that depends on a “first” set item. A set has no position-based indexing like a list or tuple, and its displayed order is not the point.

Trace dictionary iteration and accumulation
scores = {"Ana": 8, "Marko": 6, "Jelena": 10}
total = 0

for name, score in scores.items():
    total += score

print(total)
print(len(scores))

Start at total = 0. Ana / 8 makes 8; Marko / 6 makes 14; Jelena / 10 makes 24. items() supplies each key-value pair, unpacking puts the number in score, and len(scores) counts three pairs.

Choose the iteration form by your intent

  1. One value from a list or tupleUse for item in collection when you want one ordered item at a time.
  2. Each member of a setUse for item in set_name to visit every member without relying on positions or order.
  3. Dictionary keys, values, or bothUse for key in dictionary for keys, dictionary.values() for values, and dictionary.items() with key, value when you need both.

Common beginner mistakes

  • Treating the loop variable as the whole collectionIn for name in names, name is one current name, not the entire list.
  • Expecting a dictionary loop to give valuesIn for item in student, item is a key. Use student.values() for values.
  • Unpacking something that is not a pairfor key, value in student.items() works because each item is a two-part key-value pair.
  • Depending on set orderDo not write logic around a “first” set item. Use a list or tuple when position and order matter.
  • Forgetting indentationCode that belongs to the loop must be indented, or Python cannot tell which statement should run for each item.

Python Playground

Loading Python runtime…

Before running the code, predict what <code>key</code> and <code>value</code> represent. Then add or change one dictionary pair, run it again, and observe the new iteration.

iterating_over_collections.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

numbers = [2, 4, 6]
total = 0

for number in numbers:
    total += number

print(total)
print(len(numbers))
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.