How to Write a for Loop in Python
What you’ll build or solve
You’ll write for loops that iterate over lists, strings, and ranges, and you’ll control the loop with clear, readable patterns.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Process each item in a list, like names, prices, or tasks.
- Repeat an action a set number of times, like 10 retries or 12 months.
- Walk through text one character at a time, like counting letters.
Avoid this approach when:
- You need to repeat until a condition changes. A
whileloop fits better. - You need heavy numeric loops for performance. Vectorized tools may be faster.
Prerequisites
- Python 3 installed
- You know what variables are
- You know what a list is
- You can run a short Python script
Step-by-step instructions
1) Loop through a list
Use for item in my_list: when you want each element.
Python
names = ["Ana", "Bo", "Chen"]
for name in names:
print(name)
Python takes each value from names and assigns it to name one by one.
2) Loop through a string
A string is iterable, so the loop gives you one character at a time.
Python
word = "mimo"
for ch in word:
print(ch)
Each character becomes the loop variable ch.
3) Loop a fixed number of times with range()
Use range(n) to loop from 0 up to n - 1.
Python
for i in range(3):
print(i)
If you want 1 to 3 instead:
Python
for i in range(1, 4):
print(i)
What to look for:
The end value is not included.
4) Get both index and value with enumerate()
Use this when you need positions and items.
Python
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(index, color)
If you want the counting to start at 1:
Python
for index, color in enumerate(colors, start=1):
print(index, color)
5) Loop through a dictionary
Use .items() to loop through key-value pairs.
Python
prices = {"apple": 1.2, "banana": 0.8}
for item, price in prices.items():
print(item, price)
If you only need keys, use for key in prices:.
If you only need values, use for value in prices.values():.
6) Add simple loop control with break and continue
These statements work in any loop.
Python
numbers = [2, 5, 0, 7]
for n in numbers:
if n == 0:
continue
if n == 7:
break
print(n)
continueskips to the next iteration.breakstops the loop entirely.
Examples you can copy
1) Sum numbers in a list
Python
nums = [3, 5, 2]
total = 0
for n in nums:
total += n
print(total)
2) Find the first matching item
Python
names = ["Ana", "Bo", "Chen", "Dina"]
target = "Chen"
found = False
for name in names:
if name == target:
found = True
break
print(found)
3) Build a new list from an old one
Python
scores = [55, 70, 49, 88]
passing = []
for s in scores:
if s >= 60:
passing.append(s)
print(passing)
Common mistakes and how to fix them
Mistake 1: Forgetting the colon or indentation
You might write:
Python
names = ["Ana", "Bo"]
for name in names
print(name)
Why it breaks:
Python requires : after the loop header and indentation for the loop body.
Correct approach:
Python
names = ["Ana", "Bo"]
for name in names:
print(name)
Mistake 2: Changing the list while looping over it
You might write:
Python
nums = [1, 2, 3, 4]
for n in nums:
if n % 2 == 0:
nums.remove(n)
print(nums)
Why it breaks:
Removing items shifts indexes, so the loop can skip elements.
Correct approach:
Build a new list instead.
Python
nums = [1, 2, 3, 4]
kept = []
for n in nums:
if n % 2 != 0:
kept.append(n)
print(kept)
Mistake 3: Off-by-one errors with range()
You might write:
Python
for i in range(1, 3):
print(i)
Why it breaks:
The end value is excluded, so this prints 1 and 2, not 3.
Correct approach:
Python
for i in range(1, 4):
print(i)
Troubleshooting
If you see IndentationError, make sure the loop body is indented consistently, either all spaces or all tabs.
If you see SyntaxError on the for line, check for a missing colon : at the end.
If your loop never stops, confirm you did not accidentally use a while loop. A for loop ends when the iterable ends.
If items seem skipped, check if you remove from the same list you are looping over. Build a new list instead.
Quick recap
- Use
for item in items:to loop through a list. - Use
for ch in text:to loop through characters in a string. - Use
range()for a fixed number of repeats. - Use
enumerate()for index plus value. - Use
.items()to loop through dictionary keys and values. - Use
breakto stop early andcontinueto skip items.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot