🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

itertools Module

AI & DATA SCIENCE // itertools-module

The itertools module provides fast, memory-efficient building blocks for working with iterators — combinations, permutations, infinite counters, and chaining sequences together.

Syntax

import itertools
itertools.chain(a, b)
itertools.combinations(items, 2)
itertools.count(start=0)
itertools.cycle(items)

Deep Dive Course

Every function in itertools returns a lazy iterator rather than a materialized list, so they compose well and stay memory-efficient even over huge or infinite sequences. itertools.chain(a, b, c) iterates over several iterables back-to-back as if they were one, without copying them together into a new list first. itertools.combinations(items, r) and itertools.permutations(items, r) generate every possible grouping or ordering of a given size. itertools.count() and itertools.cycle() produce infinite sequences, a counting sequence and a repeating loop over a given collection respectively, meant to be paired with something like itertools.islice() or a break condition to avoid iterating forever.

1Understanding itertools Module

Every function in itertools returns a lazy iterator rather than a materialized list, so they compose well and stay memory-efficient even over huge or infinite sequences. itertools.chain(a, b, c) iterates over several iterables back-to-back as if they were one, without copying them together into a new list first. itertools.combinations(items, r) and itertools.permutations(items, r) generate every possible grouping or ordering of a given size. itertools.count() and itertools.cycle() produce infinite sequences, a counting sequence and a repeating loop over a given collection respectively, meant to be paired with something like itertools.islice() or a break condition to avoid iterating forever.

💡

Use itertools.chain() instead of concatenating lists with + when combining several iterables just to loop over them once — chain() avoids building an intermediate combined list in memory.

editor.html
import itertools

for pair in itertools.combinations(["A", "B", "C"], 2):
    print(pair)
localhost:3000

2Practical Example

Here is a real-world application of itertools Module showing how it is used in production Python code.

editor.html
import itertools

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list(itertools.chain(list1, list2))
print(combined)
localhost:3000

3Best Practices

Follow these guidelines when working with itertools Module:

1. Use itertools.chain() to iterate over multiple sequences as one, instead of concatenating them into a new list first

2. Use itertools.combinations()/permutations() instead of writing nested loops by hand to generate groupings or orderings

3. Pair infinite generators like itertools.count()/cycle() with itertools.islice() or an explicit break, since they never stop on their own

⚠️

Tip: Use itertools.chain() instead of concatenating lists with + when combining several iterables just to loop over them once — chain() avoids building an intermediate combined list in memory.

editor.html
import itertools

for pair in itertools.combinations(["A", "B", "C"], 2):
    print(pair)
localhost:3000

Examples

Example 01Basic Usage
import itertools

for pair in itertools.combinations(["A", "B", "C"], 2):
    print(pair)
Example 02Advanced Example
import itertools

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list(itertools.chain(list1, list2))
print(combined)

Best Practices

  • Use itertools.chain() to iterate over multiple sequences as one, instead of concatenating them into a new list first
  • Use itertools.combinations()/permutations() instead of writing nested loops by hand to generate groupings or orderings
  • Pair infinite generators like itertools.count()/cycle() with itertools.islice() or an explicit break, since they never stop on their own

Interview Question

Why would you use itertools.chain() instead of just concatenating two lists with + to iterate over both?

Hint: Think about memory usage, especially for large or lazy sequences.

Concatenating two lists with + immediately allocates a brand-new list containing a full copy of both, which costs extra memory proportional to their combined size, and only works when both sides are already concrete lists. itertools.chain() instead produces a lazy iterator that visits each element of the first iterable and then the second in turn, without ever building a combined list in memory, and it works on any iterables, not just lists, including other lazy generators — which matters more as the inputs get large or are themselves lazily generated.

Exercises

MediumPractice using itertools Module in a real scenario.
View Solution
import itertools

for pair in itertools.combinations(["A", "B", "C"], 2):
    print(pair)

Frequently Asked Questions

Why would you use itertools.chain() instead of just concatenating two lists with + to iterate over both?

Concatenating two lists with + immediately allocates a brand-new list containing a full copy of both, which costs extra memory proportional to their combined size, and only works when both sides are already concrete lists. itertools.chain() instead produces a lazy iterator that visits each element of the first iterable and then the second in turn, without ever building a combined list in memory, and it works on any iterables, not just lists, including other lazy generators — which matters more as the inputs get large or are themselves lazily generated.

Related Functions

for-loopgenerators-yieldlist-comprehensions