How to Iterate Through a List in Python
What you’ll build or solve
You’ll loop through a Python list and do something with each item, like print it, transform it, or filter it.
When this approach works best
Iterating through a list works well when you:
Learn Python on Mimo
- Process each item in a dataset, like names, file paths, or API results.
- Transform values, like converting strings to numbers or normalizing text.
- Filter items, like keeping only valid entries or removing duplicates.
Skip this approach when you can use built-in functions that already express your goal, like sum(), any(), or a list comprehension for simple transforms. Those are often shorter and easier to read.
Prerequisites
- Python 3 installed
- You know what a list is
Step-by-step instructions
1) Loop through items with for
A for loop is the most common way to iterate through a list.
CSS
names= ["Amina","Luka","Noor"]
fornameinnames:
print(name)
What to look for: The loop variable (name) takes on each value from the list, one at a time.
2) Get the index and the item
Use enumerate() when you need both the position and the value.
names= ["Amina","Luka","Noor"]
fori,nameinenumerate(names):
print(i,name)
If you want human-friendly numbering, start at 1.
names= ["Amina","Luka","Noor"]
fori,nameinenumerate(names,start=1):
print(i,name)
What to look for: enumerate(..., start=1) changes the first index from 0 to 1.
3) Iterate with a condition or transformation
If you’re building a new list, iterate and collect results.
Option A (common): list comprehension
scores= [10,0,25,30]
non_zero= [sforsinscoresifs!=0]
print(non_zero)
Option B: for loop with append() when you need more logic
CSS
scores= [10,0,25,30]
non_zero= []
forsinscores:
ifs!=0:
non_zero.append(s)
print(non_zero)
What to look for: List comprehensions work best for simple transforms or filters. Use a for loop when you need multiple steps or more complex logic.
Examples you can copy
Example 1: Transform each item
words= [" apple ","Banana","CHERRY"]
cleaned= [w.strip().lower()forwinwords]
print(cleaned)
Example 2: Build a dictionary from a list
names= ["Amina","Luka","Noor"]
name_to_len= {}
fornameinnames:
name_to_len[name]=len(name)
print(name_to_len)
Example 3: Filter out empty values
items= ["ok",""," ","valid"]
filtered= [xforxinitemsifx.strip()]
print(filtered)
Example 4: Stop early when you find a match
numbers= [3,8,12,7,5]
forninnumbers:
ifn%2==0:
first_even=n
break
else:
first_even=None
print(first_even)
What to look for: The else block runs only if the loop doesn’t hit break.
Example 5: Iterate over two lists at once
names= ["Amina","Luka","Noor"]
scores= [42,38,50]
forname,scoreinzip(names,scores):
print(name,score)
What to look for: zip() stops at the shortest list. If lengths can differ, decide how you want to handle extra items.
Common mistakes and how to fix them
Mistake 1: Modifying a list while iterating over it
What you might do:
nums= [1,2,3,4]
forninnums:
ifn%2==0:
nums.remove(n)
Why it breaks: Removing items shifts the remaining elements, so you can skip values.
Correct approach: Create a new list instead.
nums= [1,2,3,4]
nums= [nforninnumsifn%2!=0]
print(nums)
Mistake 2: Using range(len(list)) when you don’t need indexes
What you might do:
names= ["Amina","Luka","Noor"]
foriinrange(len(names)):
print(names[i])
Why it breaks: It’s easy to make off-by-one mistakes, and the code is harder to read than a direct loop.
Correct approach:
CSS
names= ["Amina","Luka","Noor"]
fornameinnames:
print(name)
If you need indexes, use enumerate():
fori,nameinenumerate(names):
print(i,name)
Mistake 3: Forgetting break and doing extra work
What you might do:
numbers= [3,8,12,7,5]
first_even=None
forninnumbers:
ifn%2==0:
first_even=n
Why it breaks: You keep looping, so you end up with the last even number, not the first one.
Correct approach:
numbers= [3,8,12,7,5]
first_even=None
forninnumbers:
ifn%2==0:
first_even=n
break
Troubleshooting
If you see TypeError: 'int' object is not iterable, you’re looping over a single value, not a list. Print the variable and confirm it’s a list.
If your loop misses items, you might be changing the list while iterating. Filter into a new list instead.
If you get IndexError, check if you used indexes like names[i] with a bad range. Prefer for item in items or enumerate().
If nothing prints, confirm the list isn’t empty and confirm your code block actually runs.
Quick recap
- Use
for item in itemsfor the most readable list iteration. - Use
enumerate(items)when you need indexes. - Use a list comprehension for simple filtering or transformation.
- Avoid mutating a list while iterating over it.
- Use
breakwhen you want to stop after finding what you need.
Join 35M+ 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