Iteration in Python means stepping through each item of an iterable object — such as a list, tuple, string, dictionary, set, range, or even a file — one element at a time. It’s the foundation of working with collections of data: processing every item, transforming values, filtering, aggregating, or printing them. Python makes iteration elegant and efficient with the for loop, which is the most common and readable way to iterate.
In 2026, iteration is everywhere — from simple loops to list comprehensions, generators, map()/filter(), and modern tools like Polars. Here’s a complete, practical guide to what iteration is, how it works, and the best ways to use it in real code.
The simplest form is the for loop over any iterable. Python automatically handles getting the next item until there are no more — no manual indexing needed.
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 both the item and its position, use enumerate() — it pairs each element with a counter (starting at 0 by default, or any number you choose).
for index, fruit in enumerate(fruits, start=1):
print(f"{index}. {fruit}")
# Output:
# 1. apple
# 2. banana
# 3. cherry
# 4. date
Iterating over strings works character by character — useful for text processing.
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]. Use .keys(), .values(), or .items() for explicit control.
person = {"name": "Alice", "age": 30, "city": "New York"}
# Iterate keys (default)
for key in person:
print(key, "?", person[key])
# Iterate key-value pairs (most common)
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, or database results — iteration is how you handle every record.
import csv
with open("sales.csv", "r") as f:
reader = csv.DictReader(f)
total = 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 iteration clean, efficient, and safe. Prefer for item in iterable over index-based loops — it’s more readable and faster. Use enumerate() when you need positions — avoid manual counters. Iterate dictionaries with .items() — clearer than dict[key] lookups. Avoid modifying the iterable while iterating — it can cause skips or errors; collect changes in a new list instead. Use comprehensions for simple transformations — they’re often faster and more Pythonic than map()/filter() + loops. Modern tip: for very large data, use generators (yield) or Polars lazy frames — they iterate without loading everything into memory. In production, wrap iteration over external data (files, APIs) in try/except — handle bad rows gracefully without crashing the whole process.
Iteration is how Python turns collections into results — one item at a time, cleanly and efficiently. In 2026, master the for loop, enumerate(), .items(), comprehensions, and safe error handling. Whether you’re processing lists, files, APIs, or databases, iteration is your most used tool — make it readable, robust, and Pythonic.
Next time you have a collection of data — reach for a for loop. It’s Python’s simplest, most powerful way to say: “Do this for every item.”