Skip to main content
Editor

Python itertools: Powerful Tools for Working with Iterables

Advanced30 min7 exercises105 XP
0/7 exercises

Imagine you have a Swiss Army knife for loops. Instead of writing complicated nested loops and temporary lists, you pull out the right tiny tool and the job is done in one clean line.

That's what itertools is. It's a standard library module packed with functions that create efficient iterators for looping. They're memory-friendly because they produce items one at a time instead of building huge lists.

In this tutorial, you'll learn the most useful itertools functions: chain, product, permutations, combinations, islice, takewhile/dropwhile, groupby, and accumulate. Each one replaces several lines of manual loop code.

How Does chain Combine Multiple Iterables?

Sometimes you have several lists and you want to loop through all of them as if they were one big list. You could concatenate them with +, but that creates a whole new list in memory. chain just connects them end-to-end without copying anything.

Chaining multiple lists together
from itertools import chain
fruits = ['apple', 'banana']
veggies = ['carrot', 'pea']
grains = ['rice', 'oat']
# Loop through all three as one sequence
all_foods = list(chain(fruits, veggies, grains))
print(all_foods)
Output
['apple', 'banana', 'carrot', 'pea', 'rice', 'oat']

If you have a list of lists (a nested list), use chain.from_iterable() to flatten it into a single sequence.

Flattening a list of lists
from itertools import chain
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat = list(chain.from_iterable(nested))
print(flat)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]

What Is the Cartesian Product and When Do You Need It?

Have you ever made a table of all possible combinations? Like pairing every shirt color with every pants color? That's a Cartesian product. Instead of writing nested loops, product() does it for you.

All color-size combinations
from itertools import product
colors = ['red', 'blue']
sizes = ['S', 'M', 'L']
combos = list(product(colors, sizes))
for combo in combos:
print(combo)
Output
('red', 'S')
('red', 'M')
('red', 'L')
('blue', 'S')
('blue', 'M')
('blue', 'L')

The repeat parameter lets you take the product of an iterable with itself. This is useful for things like generating all possible dice rolls or binary strings.

Product with repeat
from itertools import product
# All possible 2-digit binary numbers
bits = list(product([0, 1], repeat=3))
for b in bits:
print(b)
Output
(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
Nested loops (manual)
combos = []
for x in [1, 2]:
    for y in ['a', 'b']:
        combos.append((x, y))
print(combos)
itertools.product (clean)
from itertools import product
combos = list(product([1, 2], ['a', 'b']))
print(combos)

How Do permutations and combinations Differ?

Think of a locker combination. With permutations, the order matters: 1-2-3 is different from 3-2-1. With combinations, the order doesn't matter: picking cards {A, K, Q} is the same no matter which order you drew them.

Permutations: order matters
from itertools import permutations, combinations
letters = ['A', 'B', 'C']
# Permutations: order matters
print('Permutations (length 2):')
for p in permutations(letters, 2):
print(p)
Output
Permutations (length 2):
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')
Combinations: order does not matter
from itertools import combinations
letters = ['A', 'B', 'C']
# Combinations: order does NOT matter
print('Combinations (length 2):')
for c in combinations(letters, 2):
print(c)
Output
Combinations (length 2):
('A', 'B')
('A', 'C')
('B', 'C')

Notice that permutations gives both ('A', 'B') and ('B', 'A'), while combinations only gives ('A', 'B'). Permutations always produces more results because each arrangement counts as different.

There's also combinations_with_replacement() which allows the same item to appear more than once. Think of it like scooping ice cream where you can pick the same flavor twice.

Combinations with replacement
from itertools import combinations_with_replacement
flavors = ['vanilla', 'chocolate', 'strawberry']
print('2-scoop combos (can repeat):')
for combo in combinations_with_replacement(flavors, 2):
print(combo)
Output
2-scoop combos (can repeat):
('vanilla', 'vanilla')
('vanilla', 'chocolate')
('vanilla', 'strawberry')
('chocolate', 'chocolate')
('chocolate', 'strawberry')
('strawberry', 'strawberry')

How Can You Slice Iterators with islice, takewhile, and dropwhile?

You can slice a list with my_list[2:5], but you can't slice an iterator that way. islice lets you grab a portion of any iterator without converting it to a list first.

Slicing iterators with islice
from itertools import islice
# islice(iterable, stop) or islice(iterable, start, stop, step)
letters = 'abcdefghij'
# Get items 2 through 5
result = list(islice(letters, 2, 6))
print('Slice [2:6]:', result)
# Get first 4 items
first_four = list(islice(letters, 4))
print('First 4:', first_four)
# Every other item from the first 8
every_other = list(islice(letters, 0, 8, 2))
print('Every other:', every_other)
Output
Slice [2:6]: ['c', 'd', 'e', 'f']
First 4: ['a', 'b', 'c', 'd']
Every other: ['a', 'c', 'e', 'g']

takewhile grabs items from the start of a sequence as long as a condition is true. The moment the condition fails, it stops. dropwhile is the opposite: it skips items while the condition is true, then gives you everything after.

takewhile and dropwhile
from itertools import takewhile, dropwhile
numbers = [2, 4, 6, 1, 3, 8, 10]
# Take while the number is even
result1 = list(takewhile(lambda x: x % 2 == 0, numbers))
print('takewhile even:', result1)
# Drop while the number is even, keep the rest
result2 = list(dropwhile(lambda x: x % 2 == 0, numbers))
print('dropwhile even:', result2)
Output
takewhile even: [2, 4, 6]
dropwhile even: [1, 3, 8, 10]

How Does groupby Organize Data into Groups?

groupby groups consecutive items that share the same key. Think of sorting a deck of cards by suit: you go through the deck and group consecutive cards that are the same suit.

Grouping animals by type
from itertools import groupby
# Data must be sorted by the grouping key!
animals = [
('cat', 'mammal'),
('dog', 'mammal'),
('eagle', 'bird'),
('parrot', 'bird'),
('snake', 'reptile'),
]
for animal_type, group in groupby(animals, key=lambda x: x[1]):
members = [name for name, _ in group]
print(f'{animal_type}: {members}')
Output
mammal: ['cat', 'dog']
bird: ['eagle', 'parrot']
reptile: ['snake']
Grouping words by length
from itertools import groupby
# Group words by their length
words = ['hi', 'go', 'cat', 'dog', 'fish', 'bird', 'elephant']
words.sort(key=len) # Sort by length first!
for length, group in groupby(words, key=len):
word_list = list(group)
print(f'Length {length}: {word_list}')
Output
Length 2: ['hi', 'go']
Length 3: ['cat', 'dog']
Length 4: ['fish', 'bird']
Length 8: ['elephant']

What Does accumulate Do with Running Totals?

accumulate produces running totals (or running results of any function). Think of a bank account: you start with a balance, and each transaction adds to or subtracts from the running total.

Running sum with accumulate
from itertools import accumulate
# Running total (default behavior)
payments = [100, 200, 50, 300, 75]
running_total = list(accumulate(payments))
print('Running total:', running_total)
Output
Running total: [100, 300, 350, 650, 725]

You can pass a custom function to accumulate. For example, you can compute a running maximum or running product.

Custom accumulate functions
from itertools import accumulate
import operator
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
# Running maximum
running_max = list(accumulate(numbers, max))
print('Running max:', running_max)
# Running product
small = [1, 2, 3, 4, 5]
running_product = list(accumulate(small, operator.mul))
print('Running product:', running_product)
Output
Running max: [3, 3, 4, 4, 5, 9, 9, 9]
Running product: [1, 2, 6, 24, 120]

Practice Exercises

Flatten a Nested List
Write Code

Use chain.from_iterable to flatten the nested list [[1, 2], [3, 4], [5, 6]] into a single flat list. Print the result.

from itertools import chain
nested = [[1, 2], [3, 4], [5, 6]]
# Flatten and print
Generate All Coin Flip Outcomes
Write Code

Use product to generate all possible outcomes of flipping a coin 3 times. The coin has sides 'H' and 'T'. Print each outcome tuple on its own line.

from itertools import product
# Generate and print all 3-flip outcomes
Find All 2-Letter Permutations
Write Code

Use permutations to find all 2-letter arrangements from the letters ['X', 'Y', 'Z']. Convert the result to a list and print it.

from itertools import permutations
letters = ['X', 'Y', 'Z']
# Find all 2-letter permutations and print
Predict the takewhile Output
Predict Output

What does this code print? Remember that takewhile stops at the FIRST item that fails the condition.

from itertools import takewhile
nums = [1, 3, 5, 2, 4, 7]
result = list(takewhile(lambda x: x < 5, nums))
print(result)
from itertools import takewhile
nums = [1, 3, 5, 2, 4, 7]
result = list(takewhile(lambda x: x < 5, nums))
print(result)
Compute a Running Total
Write Code

Use accumulate to compute the running total of [10, 20, 30, 40, 50]. Convert to a list and print it.

from itertools import accumulate
values = [10, 20, 30, 40, 50]
# Compute running total and print
Group Numbers as Even or Odd
Write Code

Given numbers = [1, 3, 2, 4, 5, 7, 6, 8], sort them by whether they're even or odd, then use groupby to print each group. Use lambda x: x % 2 == 0 as the key. Print in the format:

False: [1, 3, 5, 7]

True: [2, 4, 6, 8]

from itertools import groupby
numbers = [1, 3, 2, 4, 5, 7, 6, 8]
# Sort by even/odd, then group and print
Fix the groupby Bug
Fix the Bug

This code tries to group words by first letter, but the output is wrong because the data isn't sorted. Fix the code so it correctly groups all words by their first letter.

Expected output:

a: ['ant', 'ape']

b: ['bat', 'bee']

c: ['cat', 'cow']

from itertools import groupby
words = ['cat', 'ant', 'bat', 'ape', 'cow', 'bee']
# Bug: data not sorted before groupby!
for letter, group in groupby(words, key=lambda w: w[0]):
print(f'{letter}: {list(group)}')

Harder problems

More problems on this topic: Iterators and generators

Related Tutorials

Was this tutorial helpful?