← Back to Blog

Going Through Every Element

The problem...

You have a list. Ten elements. A hundred. Maybe more.

You don't want to write print(squad[0]), print(squad[1]), print(squad[2])...

You want to do something with every element — automatically.

The idea!

You already know for loops. You used them with strings and ranges.

Lists work exactly the same way.

Python moves through the list one element at a time, and your loop variable holds the current one.

The basic loop

squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]

for member in squad:
    print(member)

Output:

Raven
Wolf
Ghost
Viper
Bull

One iteration per element. Top to bottom. No index needed.

Doing something with each element

scores = [85, 74, 91, 63, 98]

for score in scores:
    if score >= 80:
        print(f"{score} — passed")

Output:

85 — passed
91 — passed
98 — passed

When you need the index too

Sometimes you need both the position and the value. Use enumerate():

squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]

for index, member in enumerate(squad):
    print(f"{index} — {member}")

Output:

0 — Raven
1 — Wolf
2 — Ghost
3 — Viper
4 — Bull

If you want counting to start at 1, pass a start value:

for index, member in enumerate(squad, 1):
    print(f"{index}. {member}")

Output:

1. Raven
2. Wolf
3. Ghost
4. Viper
5. Bull

Looping over two lists at once

Use zip() to pair elements from two lists:

names = ["Raven", "Wolf", "Ghost"]
scores = [85, 74, 91]

for name, score in zip(names, scores):
    print(f"{name}: {score}")

Output:

Raven: 85
Wolf: 74
Ghost: 91

zip() stops at the shortest list. If one list is longer, the extra elements are ignored.

Looping with a while loop

You can also loop through a list using a while loop and a manual index:

squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
i = 0

while i < len(squad):
    print(squad[i])
    i += 1

You'll reach for this when you need more control — skipping elements, going backwards, stopping early based on logic.

For straightforward iteration, for is cleaner.

What's really happening

A for loop on a list asks Python for the next element on each iteration.

It doesn't use an index internally — it just moves forward through the list until there's nothing left.

The list is never modified by the loop itself.

Heads up!

  • Don't modify a list while looping over it — the results are unpredictable
  • Use enumerate() when you need the index — don't use range(len(list)) for this
  • zip() stops at the shortest list — no error, just silence on the extra elements
  • The loop variable name is yours — pick something readable

The mindset shift

Stop thinking: "I need the index to get each element."

Start thinking: "The loop gives me each element directly. I only need the index when the index itself matters."

What you should understand now

  • A for loop moves through every element in a list automatically
  • The loop variable holds the current element on each iteration
  • Use enumerate() when you need both the index and the value
  • Use zip() to loop over two lists in parallel
  • A while loop gives more control when you need it
[ login to bookmark ] // copied! 19 views · 2 min
// resources
Code Example list_iteration.py
← prev How to Inspect a List next → Going Deeper with Nested Lists
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.