🕯️ Magic Note
The functions enumerate(), zip(), reversed(), and sorted() all return iterators. They do not create new lists in memory. They generate values one at a time as you loop. This makes them memory efficient even for very large data.
Python
fruits = [“apple”, “banana”, “cherry”, “date”]
# Bad way (C-style)
for i in range(len(fruits)):
print(f”{i}: {fruits[i]}”)
# Pythonic way
for i, fruit in enumerate(fruits):
print(f”{i}: {fruit}”)
# Start index from 1 instead of 0
for i, fruit in enumerate(fruits, start=1):
print(f”{i}: {fruit}”)
# 1: apple
# 2: banana
# 3: cherry
# 4: date
Python
names = [“Ali”, “Sara”, “Reza”, “Mina”]
scores = [95, 87, 92, 88]
ages = [25, 30, 28, 24]
# Loop through two lists
for name, score in zip(names, scores):
print(f”{name}: {score}”)
# Loop through three lists
for name, score, age in zip(names, scores, ages):
print(f”{name} | Score: {score} | Age: {age}”)
# Create a dictionary from two lists
name_score_dict = dict(zip(names, scores))
print(name_score_dict)
# {“Ali”: 95, “Sara”: 87, “Reza”: 92, “Mina”: 88}
Python
from itertools import zip_longest
names = [“Ali”, “Sara”, “Reza”]
scores = [95, 87]
# zip() stops at shorter list
for name, score in zip(names, scores):
print(name, score) # Only prints Ali and Sara
# zip_longest fills missing values
for name, score in zip_longest(names, scores, fillvalue=0):
print(name, score) # Reza gets 0
Python
colors = [“red”, “green”, “blue”, “yellow”]
# Loop in reverse order
for color in reversed(colors):
print(color)
# yellow
# blue
# green
# red
# reverse with enumerate (get index from the end)
for i, color in enumerate(reversed(colors)):
print(f”{len(colors) – i – 1}: {color}”)
🕯️ Magic Note
reversed() does not create a new list. It returns an iterator that traverses the original list backwards. This is memory efficient.
Python
numbers = [5, 2, 8, 1, 9, 3]
names = [“Sara”, “Ali”, “Reza”, “Mina”]
# Loop in ascending order
for num in sorted(numbers):
print(num, end=” “)
# 1 2 3 5 8 9
# Loop in descending order
for num in sorted(numbers, reverse=True):
print(num, end=” “)
# 9 8 5 3 2 1
# Sort strings alphabetically
for name in sorted(names):
print(name)
# Ali
# Mina
# Reza
# Sara
# Original list remains unchanged
print(numbers) # [5, 2, 8, 1, 9, 3] (still original order)
Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Loop through even numbers only
for num in numbers:
if num % 2 == 0:
print(num, end=” “)
# 2 4 6 8 10
# Using filter() function (functional style)
for num in filter(lambda x: x % 2 == 0, numbers):
print(num, end=” “)
# 2 4 6 8 10
🕯️ Magic Note
List comprehensions can also filter. Write [x for x in numbers if x % 2 == 0] to create a filtered list. Then loop over it.
Python
# break: stop when condition is met
for num in range(1, 100):
if num ** 2 > 50:
break
print(f”{num}^2 = {num**2}”)
# Stops at 8^2 = 64 (first square to exceed 50 is 7^2=49, 8^2=64)
# continue: skip current iteration
for num in range(1, 11):
if num % 2 == 0:
continue
print(num, end=” “)
# 1 3 5 7 9 (skips even numbers)
Python
# Find a prime number
num = 17
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f”{num} is not prime (divisible by {i})”)
break
else:
print(f”{num} is prime”)
# Output: 17 is prime
# Search in a list
colors = [“red”, “green”, “blue”, “yellow”]
target = “purple”
for color in colors:
if color == target:
print(f”Found {target}”)
break
else:
print(f”{target} not found”)
# Output: purple not found
Python
person = {“name”: “Feloriya”, “age”: 25, “city”: “Tehran”}
# Bad way (looks up value each time)
for key in person:
print(f”{key}: {person[key]}”)
# Good way (direct unpacking)
for key, value in person.items():
print(f”{key}: {value}”)
Python
# List of tuples
points = [(1, 2), (3, 4), (5, 6)]
for x, y in points:
print(f”x: {x}, y: {y}”)
# List of lists
coordinates = [[10, 20], [30, 40], [50, 60]]
for x, y in coordinates:
print(f”x: {x}, y: {y}”)
🕯️ Magic Note
Unpacking works in loops just like it works in assignments. Python automatically assigns each element of the inner sequence to the corresponding variable.
Python
items = [10, 20, 30, 40, 50]
# Skip first item
for item in items[1:]:
print(item, end=” “)
# 20 30 40 50
# Skip last item
for item in items[:-1]:
print(item, end=” “)
# 10 20 30 40
# Skip first and last
for item in items[1:-1]:
print(item, end=” “)
# 20 30 40
Python
numbers = [1, 3, 5, 7, 9]
# Manual way
for i in range(len(numbers) – 1):
print(f”{numbers[i]} -> {numbers[i+1]}”)
# Using zip (more Pythonic)
for a, b in zip(numbers, numbers[1:]):
print(f”{a} -> {b}”)
# 1 -> 3
# 3 -> 5
# 5 -> 7
# 7 -> 9
Python
from itertools import cycle, combinations, product
# cycle: repeat endlessly
colors = [“red”, “green”, “blue”]
for color in cycle(colors):
print(color)
# red, green, blue, red, green, blue … (infinite)
# combinations: all unique pairs
names = [“Ali”, “Sara”, “Reza”]
for a, b in combinations(names, 2):
print(f”{a} – {b}”)
# Ali – Sara
# Ali – Reza
# Sara – Reza
# product: Cartesian product
suits = [“hearts”, “diamonds”]
values = [“A”, “K”]
for suit, value in product(suits, values):
print(f”{value} of {suit}”)
# A of hearts, K of hearts, A of diamonds, K of diamonds
- Using range(len(list)) instead of enumerate()
- Modifying a list while iterating over it (use a copy or collect changes)
- Forgetting that zip() stops at the shortest iterable
- Using reversed() on a set (sets have no order)
- Confusing sorted() (returns new list) with .sort() (modifies in place)
- Using else with a loop without understanding its behavior
- How do you loop through a list with both index and value?
- Write code that loops through two lists names and scores together.
- How do you loop through a list in reverse order without modifying it?
- What is the difference between sorted() and .sort() in loops?
- Write a loop that finds if a number exists in a list and uses else to handle “not found”.
- How do you loop through adjacent pairs like [(1,2), (2,3), (3,4)]?
⚡ Whisper
The simple for loop is a seed. These patterns are the flowers that grow from it. enumerate() gives you position without counting. zip() weaves lists together like threads in a tapestry. reversed() walks backwards through time. sorted() brings order from chaos. Each pattern is a tool in your belt. You do not need to memorize them all at once. Learn one. Use it until it becomes natural. Then learn another. Soon you will see iteration problems and the pattern will appear in your mind like a whisper. That is mastery. Not knowing every tool. Knowing which tool fits the moment.