Iteration Patterns

Let’s look at commonly used iteration patterns in Python.

Iterating over a list

This is the most common pattern and you’ve seen it already.

x = ["a", "b", "c", "d"]
for a in x:
    print(a, a.upper())
a A
b B
c C
d D

We could use the same pattern with a list comprehension as well.

[a.upper() for a in x]
['A', 'B', 'C', 'D']

Iterating over a sequence of numbers

We can use the range function to iterate over a sequence of numbers.

for i in range(5):
    print(i, i*i)
0 0
1 1
2 4
3 9
4 16

And the same pattern as a list comprehension:

[i*i for i in range(5)]
[0, 1, 4, 9, 16]

Iterating over two lists together

The built-in function zip can be used to iterate over two lists togetger.

names = ["a", "b", "c", "d"]
scores = [10, 20, 30, 40]

for name, score in zip(names, scores):
    print(name, score)
a 10
b 20
c 30
d 40

Iterating over the index and the element together

There would be times when we need to use both the index and the element when iterating over a list. While it is possible to use range(len(x)) to iterate over the index and use the index to access the element, that is quite clumsy. Python provides a built-in function called enumerate to address this.

Let’s say we have a list of chapter titles.

chapters = [
    "Getting Started",
    "Functions",
    "Lists",
    "Dictionaries"
]

And we want to print the chapter number and title together.

for i, title in enumerate(chapters):
    print("Chapter", i, title)
Chapter 0 Getting Started
Chapter 1 Functions
Chapter 2 Lists
Chapter 3 Dictionaries

We may want the chapter number to start from 1. We could print i+1 instead of i.

for i, title in enumerate(chapters):
    print("Chapter", i+1, title)
Chapter 1 Getting Started
Chapter 2 Functions
Chapter 3 Lists
Chapter 4 Dictionaries

Or we can just tell enumerate to start counting from 1 instead of 0.

for i, title in enumerate(chapters, start=1):
    print("Chapter", i, title)
Chapter 1 Getting Started
Chapter 2 Functions
Chapter 3 Lists
Chapter 4 Dictionaries

We can also use this pattern in list comprehensions.

[f"Chapter {i}: {title}" for i, title in enumerate(chapters, start=1)]
['Chapter 1: Getting Started',
 'Chapter 2: Functions',
 'Chapter 3: Lists',
 'Chapter 4: Dictionaries']

Exercises

Problem: write a function vector_add to add two vectors.

>>> vector_add([1, 2, 3, 4], [10, 20, 30, 40])
[11, 22, 33, 44]
>>> sum(vector_add([1, 2, 3, 4], [10, 20, 30, 40]))
110