Iterating with a for loop is the most natural and readable way in Python to step through every item in an iterable — a list, tuple, string, dictionary, set, range, file lines, or any object that supports iteration. The for loop automatically handles getting the next item until the iterable is exhausted — no manual indexing or counters needed. It’s clean, Pythonic, and used constantly for processing data, printing, transforming, filtering, aggregating, or building new collections.
In 2026, the for loop remains the heart of iteration — from simple lists to massive datasets in pandas/Polars, file processing, API responses, and generators. Here’s a complete, practical guide to using for loops effectively: basic syntax, common iterables, real patterns, and modern best practices.
The syntax is beautifully simple: for variable in iterable: — the variable takes each item in turn, and the indented block runs once per item.
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
print(f"I like {fruit}")
# Output:
# I like apple
# I like banana
# I like cherry
# I like date
When you need the position (index) of each item, use enumerate() — it pairs every element with a counter (starting at 0 by default, or any number you set with start=).
for index, fruit in enumerate(fruits, start=1):
print(f"{index}. {fruit}")
# Output:
# 1. apple
# 2. banana
# 3. cherry
# 4. date
Strings iterate character by character — perfect for text processing, validation, or building new strings.
word = "Python"
for char in word:
print(char.upper(), end=" ") # P Y T H O N
Dictionaries iterate over keys by default — access values with dict[key]. For clarity, iterate explicitly with .keys(), .values(), or .items() (most common for key-value pairs).
person = {"name": "Alice", "age": 30, "city": "New York"}
# Default: keys only
for key in person:
print(key, "?", person[key])
# Best practice: iterate key-value pairs
for key, value in person.items():
print(f"{key.capitalize()}: {value}")
# Output:
# Name: Alice
# Age: 30
# City: New York
Real-world pattern: processing rows from CSV, JSON, database results, or API data — iteration is how you handle every record safely and efficiently.
import csv
with open("sales.csv", "r") as f:
reader = csv.DictReader(f)
total = 0.0
for row in reader:
try:
amount = float(row["amount"])
total += amount
except (ValueError, KeyError):
print(f"Skipping invalid row: {row}")
print(f"Total sales: ${total:.2f}")
Best practices make for loops clean, efficient, and safe. Prefer for item in iterable over index-based loops — it’s more readable, faster, and less error-prone. Use enumerate() when you need positions — avoid manual counters like i = 0; i += 1. Iterate dictionaries with .items() — clearer than repeated dict[key] lookups. Avoid modifying the iterable inside the loop — it can skip items or raise errors; collect changes in a new list instead. Use comprehensions for simple transformations — they’re often faster and more Pythonic than for + append(). Modern tip: for very large data, use generators (yield) or Polars lazy iteration — they process items one at a time without loading everything into memory. In production, wrap iteration over external data (files, APIs, databases) in try/except — handle bad items gracefully without crashing the whole loop.
Iteration with a for loop is Python’s simplest, most powerful way to say: “Do this for every item.” In 2026, master it with enumerate(), .items(), comprehensions, and safe error handling. Whether you’re processing small lists or massive datasets, the for loop is your go-to tool — make it readable, robust, and Pythonic.
Next time you have a collection of data — reach for a for loop. It’s Python’s cleanest way to work through items one at a time.