List Loops 🔁📋🤖
Loop through every item in a list automatically. No item gets skipped!
Day 12: List Loops — Visit Every Item Automatically!
The Problem Without List Loops
Imagine a delivery person with 100 packages. They visit every house one by one — no house gets skipped. That's exactly what a list loop does! Instead of printing every item manually, the loop visits each one automatically.
Your First List Loop
fruits = ["Apple", "Mango", "Banana"]
for fruit in fruits:
print(fruit)
Output: Apple, Mango, Banana. The loop visits Apple first, then Mango, then Banana. fruit is just a temporary name for whichever item the loop is currently visiting!
How It Works
for = start the loop. fruit = the current item being visited. in fruits = take items from this list. Each round, fruit becomes the next item in the list until every item is visited!
Real World Connection
When Instagram loads your feed, it loops through a list of posts and displays each one. When Swiggy shows your order history, it loops through a list of past orders. When PUBG checks which players are still alive, it loops through the player list. List loops power everything!
Do Things With Each Item
scores = [85, 92, 67, 78]
for score in scores:
if score >= 90:
print(score, "Excellent!")
else:
print(score, "Good!")
Loop + condition together! This checks every score and gives feedback. This is exactly how your school's result system works!
Common Mistakes
Mistake 1 — Missing the keyword "in".
for fruit fruits:
for fruit in fruits:
Mistake 2 — Wrong indentation.
for fruit in fruits:
print(fruit)
for fruit in fruits:
print(fruit)
Mini Challenge
Mini Challenge
Create a list of 4 friends. Loop through it and print "Hey [name], you're invited to my party!" for each friend. You just built a party invitation system!
Quick Quiz
Q: What does "fruit" represent in "for fruit in fruits"? A: The current item being visited in that loop round!
Q: Does a list loop skip any items? A: Never! Every single item gets visited.
Q: What two things must every list loop have? A: The keyword "in" and indentation inside the loop!
Key Takeaways
Key Takeaways
- for item in list visits every item in the list automatically.
- The temporary variable (fruit, score, name) holds the current item each round.
- No item ever gets skipped — the loop visits all of them.
- Always use "in" and always indent the code inside the loop.
- List loops + conditions together power almost every app feature!
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.