Fundamentals 10 min read

Introduction to Python itertools: Common Functions and Usage

This article provides a concise, English-language overview of Python's itertools library, explaining its purpose, demonstrating how its iterator‑producing functions such as accumulate, chain, combinations, compress, count, cycle, dropwhile, filterfalse, groupby, islice, permutations, product, repeat, starmap, takewhile, tee, and zip_longest work, and offering clear code examples for each.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Introduction to Python itertools: Common Functions and Usage

Many developers strive to write more Pythonic code—code that follows conventions, is easy to read, and often runs more efficiently. This article introduces the Python standard library itertools , which offers a suite of iterator‑building functions that simplify common tasks.

itertools.accumulate – Returns an iterator that yields accumulated sums (or other binary functions). >> import itertools<br/>>> x = itertools.accumulate(range(10))<br/>>> print(list(x))<br/>[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

itertools.chain – Links multiple iterables together, producing a single iterator. >> x = itertools.chain(range(3), range(4), [3, 2, 1])<br/>>> print(list(x))<br/>[0, 1, 2, 0, 1, 2, 3, 3, 2, 1]

itertools.combinations – Generates all unique combinations of a specified length from an iterable. >> x = itertools.combinations(range(4), 3)<br/>>> print(list(x))<br/>[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

itertools.combinations_with_replacement – Like combinations but allows repeated elements. >> x = itertools.combinations_with_replacement('ABC', 2)<br/>>> print(list(x))<br/>[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

itertools.compress – Filters elements from an iterable using a selector of Boolean values. >> x = itertools.compress(range(5), (True, False, True, True, False))<br/>>> print(list(x))<br/>[0, 2, 3]

itertools.count – Creates an infinite counter that can start at any number and step by any amount. >> x = itertools.count(start=20, step=-1)<br/>>> print(list(itertools.islice(x, 0, 10)))<br/>[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]

itertools.cycle – Repeats the elements of an iterable indefinitely. >> x = itertools.cycle('ABC')<br/>>> print(list(itertools.islice(x, 0, 10)))<br/>['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

itertools.dropwhile – Drops elements from an iterable as long as a predicate is true, then returns the rest. >> x = itertools.dropwhile(lambda e: e < 5, range(10))<br/>>> print(list(x))<br/>[5, 6, 7, 8, 9]

itertools.filterfalse – Returns elements for which the predicate is false. >> x = itertools.filterfalse(lambda e: e < 5, (1,5,3,6,9,4))<br/>>> print(list(x))<br/>[5, 6, 9]

itertools.groupby – Groups consecutive elements that share a common key defined by a function. >> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)<br/>>>> for condition, numbers in x:<br/>... print(condition, list(numbers))<br/>True [0, 1, 2, 3, 4]<br/>False [5, 6, 7, 8]<br/>True [9]

itertools.islice – Slices an iterator like a list slice. >> x = itertools.islice(range(10), 0, 9, 2)<br/>>> print(list(x))<br/>[0, 2, 4, 6, 8]

itertools.permutations – Generates all possible orderings of a specified length. >> x = itertools.permutations(range(4), 3)<br/>>> print(list(x))<br/>[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

itertools.product – Computes the Cartesian product of input iterables. >> x = itertools.product('ABC', range(3))<br/>>> print(list(x))<br/>[('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

itertools.repeat – Repeats a single value a specified number of times. >> x = itertools.repeat(0, 5)<br/>>> print(list(x))<br/>[0, 0, 0, 0, 0]

itertools.starmap – Applies a function to arguments unpacked from an iterable of tuples, similar to map . >> x = itertools.starmap(str.islower, 'aBCDefGhI')<br/>>> print(list(x))<br/>[True, False, False, False, True, True, False, True, False]

itertools.takewhile – Takes elements from an iterable while a predicate remains true. >> x = itertools.takewhile(lambda e: e < 5, range(10))<br/>>> print(list(x))<br/>[0, 1, 2, 3, 4]

itertools.tee – Creates multiple independent iterators from a single original iterator. >> a, b = itertools.tee(range(10), 2)<br/>>> print(list(a))<br/>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br/>>>> print(list(b))<br/>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

itertools.zip_longest – Zips iterables together, filling missing values with None when the iterables are of unequal length. >> x = itertools.zip_longest(range(3), range(5))<br/>>> print(list(x))<br/>[(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]

In conclusion, the article presents a quick reference to the most commonly used itertools functions, encouraging developers to consult the official documentation for deeper details when specific needs arise.

Functional Programmingcode examplesIteratoritertoolspythonic
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.