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
JelenaTrace 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.
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.
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.
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.
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
- One value from a list or tupleUse
for item in collectionwhen you want one ordered item at a time. - Each member of a setUse
for item in set_nameto visit every member without relying on positions or order. - Dictionary keys, values, or bothUse
for key in dictionaryfor keys,dictionary.values()for values, anddictionary.items()withkey, valuewhen you need both.
Common beginner mistakes
- Treating the loop variable as the whole collectionIn
for name in names,nameis one current name, not the entire list. - Expecting a dictionary loop to give valuesIn
for item in student,itemis a key. Usestudent.values()for values. - Unpacking something that is not a pair
for 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.
