Skip to content

Itertools Module

The itertools module in Python is a powerful utility module that provides a collection of functions for working with iterators and iterables more efficiently. These functions offer convenient tools for tasks such as creating iterators, combining and chaining iterables, and performing advanced operations on sequences.

itertools.compress(it, selector_it)

Consumes two iterables in parallel. yields items from it whenever the corresponding item in selector_it is truthy.

Example

src/builtint_modules/itertools/compress.py
import itertools

str_exe = "ABCDEFGH"
num = (True, False, True, True, False, True)

gen_obj = itertools.compress(str_exe, num)

print(list(gen_obj))
output
['A', 'C', 'D', 'F']

itertools.dropwhile(predicate, it)

Consumes it, skipping items while predicate computes truthy, then yields every remaining item (no further checks).

Example

src/builtint_modules/itertools/dropwhile.py
import itertools


def vowel(c: str):
    return c.upper() in "AEIOU"

letters = "AEIGJIOU"
gen_obj = itertools.dropwhile(vowel, letters)

print(list(gen_obj))
output
['G', 'J', 'I', 'O', 'U']

itertools.filterfalse(predicate, it)

Same as filter, with the predicate logic negated: yields items whenever predicate computes falsy.

Example

src/builtint_modules/itertools/filterfalse.py
import itertools


def vowel(c: str):
    return c.upper() in "AEIOU"

letters = "AEIGJIOU"
gen_obj = itertools.filterfalse(vowel, letters)

print(list(gen_obj))
output
['G', 'J']

itertools.islice(it, stop) or islice(it, start, stop, step=1)

Yields items from a slice of it, similar to s[:stop] or s[start:stop:step] except it can be any iterable, and the operation is lazy.

Example

src/builtint_modules/itertools/islice.py
import itertools

nums = (0,1,2,3,4,5,6,7,8,9)

gen_obj = itertools.islice(nums, 4)

print(list(gen_obj))
gen_obj = itertools.islice(nums, 4, 7)

print(list(gen_obj))

gen_obj = itertools.islice(nums, 1, 7, 2)

print(list(gen_obj))
output
[0, 1, 2, 3]
[4, 5, 6]
[1, 3, 5]

itertools.takewhile(predicate, it)

Yields items while predicate computes truthy, then stops and no further checks are made.

Example

src/builtint_modules/itertools/takewhile.py
import itertools


def check_first5(num):
    return num in (0,1,2,3,4,5)

first10 = (0,1,2,3,4,5,6,7,8,9,10)
gen_obj = itertools.takewhile(check_first5, first10)

print(list(gen_obj))
output
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

itertools.accumulate(it, [func])

Yields accumulated sums, if func is provided, yields the result of applying it to the first pair of items, then to the first result and next item, etc.

Example

src/builtint_modules/itertools/accumulate.py
import itertools
import operator

first10 = (1,2,3,4,5,6,7,8,9,10)
gen_obj = itertools.accumulate(first10)

print(list(gen_obj))
gen_obj = itertools.accumulate(first10, operator.mul)

print(list(gen_obj))
output
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]

itertools.starmap(func, it)

Applies func to each item of it, yielding the result; the input iterable should yield iterable items iit, and func is applied as func(*iit).

Example

src/builtint_modules/itertools/starmap.py
import itertools
import operator

data = [(2,3), (4,5), (6,2)]
gen_obj = itertools.starmap(operator.mul, data)

print(list(gen_obj))
output
[6, 20, 12]

itertools.chain(it1, …, itN)

Yields all items from it1, then from it2, etc., seamlessly.

Example

src/builtint_modules/itertools/chain.py
import itertools

consonants = ['d', 'f', 'k', 'l', 'n', 'p']
vowels = ['a', 'e', 'i', 'o', 'u']

gen_obj = itertools.chain(consonants, vowels)

print(list(gen_obj))
output
['d', 'f', 'k', 'l', 'n', 'p', 'a', 'e', 'i', 'o', 'u']

itertools.chain.from_iterable(it)

Yields all items from each iterable produced by it, one after the other, seamlessly. it will be an iterable where the items are also iterables, for example, a list of tuples.

Example

src/builtint_modules/itertools/chain_from_iterable.py
import itertools

vowels = ['a', 'e', 'i', 'o', 'u']

gen_obj = itertools.chain.from_iterable(enumerate(vowels))

print(list(gen_obj))
output
[0, 'a', 1, 'e', 2, 'i', 3, 'o', 4, 'u']

itertools.product(it1, …, itN, repeat=1)

Cartesian product: yields N-tuples made by combining items from each input iterable, like nested for loops could produce. repeat allows the input iterables to be consumed more than once.

Example

src/builtint_modules/itertools/product.py
import itertools

gen_obj = itertools.product("ABC", range(2))
print(list(gen_obj))

gen_obj = itertools.product("ABC", repeat=2)
print(list(gen_obj))
output
[('A', 0), ('A', 1), ('B', 0), ('B', 1), ('C', 0), ('C', 1)]
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]

itertools.zip_longest(it1, …, itN, fillvalue=None)

Yields N-tuples built from items taken from the iterables in parallel, stopping only when the last iterable is exhausted, filling the blanks with the fillvalue.

Example

src/builtint_modules/itertools/zip_longest.py
import itertools

gen_obj = itertools.zip_longest('ABC', range(5))
print(list(gen_obj))
gen_obj = itertools.zip_longest('ABC', range(5), fillvalue="!!!")
print(list(gen_obj))
output
[('A', 0), ('B', 1), ('C', 2), (None, 3), (None, 4)]
[('A', 0), ('B', 1), ('C', 2), ('!!!', 3), ('!!!', 4)]