Python itertools module tutorial
Introduction
When dealing with sequence splicing, permutation and combination, and grouping and filtering, the first reaction of many Python developers is to write nested loops, temporary lists, and complex conditional judgments—the result is often complex code, large memory usage, and average readability.
In fact, there is an "efficiency tool" hidden in the Python standard library for a long time: itertools. It provides a series of utility functions that return iterators, specifically designed to handle iterable objects efficiently. These tools can be used independently or combined like building blocks to help you write concise and elegant code without taking up a lot of memory.
This article will help you start with the three most commonly used tools and master them step by step.itertoolscore usage.
Infinite iterator
itertoolsThere are 3 built-in iterators that can "generate data infinitely". They have no default termination point. You must manually add conditions to control the end when using them, otherwise it will enter an infinite loop.
1. count() - infinite generator of arithmetic sequence
count(start=0, step=1)fromstartTo start, followstep(Supports negative numbers and floating point numbers) Output values with infinite steps. Suitable for generating natural numbers, odd sequences, timestamps and other regular sequence sequences.
2. cycle() - sequence infinite looper
cycle(iterable)An iterable object (string, list, tuple, etc.) will be repeated infinitely. It is very convenient to use it to implement carousels and infinite loop marks.
3. repeat() - single element repeater
repeat(object[, times])Repeatedly output the same object, default to infinite repetition, specifiedtimesThen it becomes a finite iterator. Commonly used to initialize placeholder lists and test batch processing functions.
Finite iterator
Next are tools for processing existing iterable objects and quick "cropping/splicing/filtering". They all stop automatically when a certain condition is encountered or elements are exhausted.
1. islice() - safe iterator slicing
islice(iterable, start, stop, step)Similar to list slicing, but operates directly on iterators and does not copy data. It is suitable for intercepting the first N items of a large data stream, which is like adding a "safety guardrail" to an infinite sequence.
2. takewhile() - conditional pre-filter
takewhile(predicate, iterable)Return elements from the beginning until the predicate function returns for the first timeFalse. with built-infilter()different:filter()will filter out all elements that meet the conditions, andtakewhile()It will terminate when it encounters the first one that is not satisfied.
3. chain() - seamless splicing of multiple iterable objects
chain(*iterables)Connect multiple iterable objects (regardless of whether the types are consistent) into an iterator in sequence, without creating an intermediate list, saving memory.
4. groupby() - adjacent element grouper
groupby(iterable, key=None)Put adjacent andkeyElements with the same value are grouped into the same group and returned(key, 组迭代器)iterator. Pay special attention to the word "adjacent": if they are the samekeyThe elements are not consecutive and will be divided into different groups. Usually you need to press the data firstkeySort** and then group.
Combining iterators
The combined iterator isitertoolsThe most commonly used and practical part of it is specially designed to generate permutation, combination, Cartesian product and other results without the need to write complex recursions or nested loops by hand.
1. product() - Cartesian product generator
product(*iterables, repeat=1)Computes the Cartesian product of multiple iterable objects. If you only have one iterable object but want to multiply itself multiple times, you can passrepeatSimplified writing of parameters.
2. permutations() - full permutation generator without duplication
permutations(iterable, r=None)Generate r length-free permutations of iterable objects (different orders are considered different results). If not specifiedr, the default is full-length arrangement.
3. combinations() - non-duplicate combination generator
combinations(iterable, r)Generate r length non-duplicate combinations (different orders are considered the same combination, and each element is only used once), must be specifiedrparameter.
Practical case: Approximate calculation of pi using infinite series
The Leibniz series is a very classic method of approximate calculation of π:
1 - 1/3 + 1/5 - 1/7 + 1/9 - …will gradually approachπ/4。
Next we useitertoolsThe tools in , implement this series summation efficiently and memory-friendly.
This code doesn't create any huge lists, odd sequences are generated on demand, the whole calculation is very memory friendly, and the logic is clear and readable - here it isitertoolscharm.
Performance considerations
itertoolsThe efficiency mainly comes from two points:
- Return an iterator instead of calculating all results at once: When processing millions or even larger data sets, the memory footprint is extremely low.
- The bottom layer is implemented in C: much faster than the equivalent pure Python loop.
In actual use, you can further combine iterators with generator expressions to avoid creating intermediate temporary lists and make the pipeline more extreme.
Summarize
itertoolsIt is Python’s “Swiss Army Knife” for processing iterable objects, mainly covering three directions:
- Generate infinite regular sequences:
count、cycle、repeat - Cut, splice, filter, and group existing sequences:
islice、takewhile、chain、groupby - Generate mathematical results such as permutation and combination, Cartesian product:
product、permutations、combinations
As long as you are familiar with these tools and can flexibly combine them according to your needs, you can make your code simpler and more efficient, and write in a truly Pythonic style.

