Python - For Loop with Examples
The for loop is Python's primary tool for iteration. Unlike C-style languages where you manually manage an index variable, Python's for loop iterates directly over items in a sequence. This...
Key Insights
- Python’s for loop uses a “for-each” pattern that iterates directly over items in any iterable, making it more readable and less error-prone than traditional index-based loops.
- The
enumerate()function eliminates the need for manual index tracking, whilerange()provides precise control over numeric sequences—master both for cleaner iteration code. - List comprehensions offer a concise alternative to for loops for simple transformations, but prioritize readability over cleverness when logic becomes complex.
Introduction to For Loops
The for loop is Python’s primary tool for iteration. Unlike C-style languages where you manually manage an index variable, Python’s for loop iterates directly over items in a sequence. This “for-each” approach reduces boilerplate and eliminates off-by-one errors.
Every Python developer uses for loops constantly—processing API responses, transforming data, reading files line by line, or building reports. Understanding the full range of iteration patterns will make your code cleaner and more Pythonic.
Basic Syntax and Iterating Over Sequences
The fundamental pattern is straightforward: for item in iterable:. Python handles the iteration mechanics; you focus on what to do with each item.
# Iterating over a list
languages = ["Python", "JavaScript", "Rust", "Go"]
for language in languages:
print(f"Learning {language}")
Output:
Learning Python
Learning JavaScript
Learning Rust
Learning Go
Strings are iterable, so you can loop through characters directly:
# Iterating over characters in a string
word = "Python"
for char in word:
print(char.upper(), end="-")
# Output: P-Y-T-H-O-N-
Tuples work identically to lists:
# Iterating over a tuple
coordinates = (10, 20, 30)
for coord in coordinates:
print(f"Position: {coord}")
The key insight: Python’s for loop works with any iterable object—lists, tuples, strings, sets, dictionaries, files, generators, and custom objects that implement the iterator protocol.
Using range() for Numeric Iteration
When you need to iterate a specific number of times or work with numeric sequences, range() is your tool. It generates numbers on demand without creating a full list in memory.
# Basic counting: 0 to 4
for i in range(5):
print(i)
# Output: 0, 1, 2, 3, 4
The range() function accepts up to three arguments: range(start, stop, step).
# Custom start and stop
for i in range(1, 6):
print(i)
# Output: 1, 2, 3, 4, 5
# With step value
for i in range(0, 10, 2):
print(i)
# Output: 0, 2, 4, 6, 8
# Reverse iteration with negative step
for i in range(10, 0, -1):
print(i)
# Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
A practical example—generating a multiplication table:
# Multiplication table for 7
number = 7
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
Remember: range() excludes the stop value. range(1, 5) gives you 1, 2, 3, 4—not 5.
Looping with Index Using enumerate()
Sometimes you need both the item and its position. Avoid the temptation to use range(len(items)) and index manually. Use enumerate() instead.
# The wrong way (avoid this)
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")
# The right way
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
Both produce the same output, but enumerate() is cleaner, more Pythonic, and less prone to index errors.
You can specify a starting index:
# Start counting from 1 instead of 0
menu_items = ["Burger", "Pizza", "Salad", "Soup"]
print("Today's Menu:")
for number, item in enumerate(menu_items, start=1):
print(f" {number}. {item}")
Output:
Today's Menu:
1. Burger
2. Pizza
3. Salad
4. Soup
Finding the index of elements that match a condition:
# Find indices of all even numbers
numbers = [3, 8, 1, 6, 2, 9, 4]
even_indices = []
for index, num in enumerate(numbers):
if num % 2 == 0:
even_indices.append(index)
print(f"Even numbers found at indices: {even_indices}")
# Output: Even numbers found at indices: [1, 3, 4, 6]
Iterating Over Dictionaries
Dictionaries require special attention because they contain key-value pairs. Python provides three methods for different iteration needs.
user = {
"name": "Alice",
"email": "alice@example.com",
"role": "admin",
"active": True
}
# Iterate over keys (default behavior)
for key in user:
print(key)
# Explicitly iterate over keys
for key in user.keys():
print(key)
# Iterate over values only
for value in user.values():
print(value)
# Iterate over both keys and values
for key, value in user.items():
print(f"{key}: {value}")
A practical example—building a formatted report from dictionary data:
# Monthly sales data
sales_data = {
"January": 15000,
"February": 18500,
"March": 22000,
"April": 19800,
"May": 25000
}
print("=" * 30)
print("MONTHLY SALES REPORT")
print("=" * 30)
total = 0
for month, amount in sales_data.items():
print(f"{month:12} | ${amount:,}")
total += amount
print("-" * 30)
print(f"{'Total':12} | ${total:,}")
Output:
==============================
MONTHLY SALES REPORT
==============================
January | $15,000
February | $18,500
March | $22,000
April | $19,800
May | $25,000
------------------------------
Total | $100,300
Loop Control: break, continue, and else
Python provides three keywords for controlling loop execution: break exits the loop entirely, continue skips to the next iteration, and else runs when the loop completes without hitting a break.
# break: exit early when condition is met
numbers = [1, 3, 5, 8, 9, 11]
for num in numbers:
if num % 2 == 0:
print(f"First even number found: {num}")
break
print(f"Checking {num}...")
Output:
Checking 1...
Checking 3...
Checking 5...
First even number found: 8
# continue: skip specific iterations
scores = [85, 92, -1, 78, 95, -1, 88]
print("Valid scores:")
for score in scores:
if score < 0:
continue # Skip invalid scores
print(f" {score}")
The for-else construct is lesser-known but useful. The else block runs only if the loop completes without a break:
# Search with for-else
def find_user(users, target_id):
for user in users:
if user["id"] == target_id:
print(f"Found: {user['name']}")
break
else:
print(f"User {target_id} not found")
users = [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
{"id": 3, "name": "Charlie"}
]
find_user(users, 2) # Output: Found: Bob
find_user(users, 5) # Output: User 5 not found
This pattern eliminates the need for a separate “found” flag variable.
List Comprehensions as For Loop Alternatives
List comprehensions provide a concise way to create lists from iterations. They’re not a replacement for all for loops, but they excel at simple transformations and filtering.
# Traditional for loop
squares = []
for x in range(1, 6):
squares.append(x ** 2)
print(squares) # [1, 4, 9, 16, 25]
# List comprehension equivalent
squares = [x ** 2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
Adding conditions:
# Filter and transform in one expression
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Even numbers doubled
even_doubled = [x * 2 for x in numbers if x % 2 == 0]
print(even_doubled) # [4, 8, 12, 16, 20]
Processing strings:
# Clean and normalize user input
raw_tags = [" Python ", "JAVASCRIPT", " rust", "Go "]
clean_tags = [tag.strip().lower() for tag in raw_tags]
print(clean_tags) # ['python', 'javascript', 'rust', 'go']
When should you use comprehensions versus explicit loops? Use comprehensions for simple, single-expression transformations. Switch to explicit loops when you need multiple statements, complex logic, or side effects like printing or writing to files.
# Good use of comprehension: simple transformation
prices = [10.50, 25.00, 8.75, 42.00]
with_tax = [price * 1.08 for price in prices]
# Better as explicit loop: complex logic with side effects
for price in prices:
taxed = price * 1.08
if taxed > 30:
print(f"High-value item: ${taxed:.2f}")
log_expensive_item(taxed)
Readability matters more than brevity. A clear three-line loop beats a cryptic one-liner every time.