In our recent discussions on generators and functional tools like map
and filter
, a common theme has emerged: the power of “lazy” and memory-efficient iteration. Python takes this concept even further with a specialized module in its standard library that is designed for exactly this purpose.
In this post, we’ll take a quick tour of the itertools
module. Think of it as a treasure chest filled with fast, memory-efficient functions for creating and working with iterators. We’ll highlight three of its most useful tools to give you a taste of what’s possible.
What is itertools
?
The itertools
module is a collection of functions inspired by functional programming languages. They are all designed to be highly efficient, operate on iterators (like lists, tuples, or generators), and promote a clean, functional style of code.
To use any of these functions, you first need to import the module at the top of your script:
import itertools
itertools.chain()
: Treating Sequences as One
What if you have multiple lists or tuples and you want to loop over all of them as if they were a single, continuous sequence? You could combine them with the +
operator, but that would create a new, larger list in memory.
The itertools.chain()
function does this lazily. It takes any number of iterables as arguments and produces an iterator that yields elements from the first one until it’s exhausted, then the second, and so on.
import itertools
list1 = [1, 2]
list2 = ["a", "b"]
tuple1 = (10, 20)
# Chain them all together into a single iterator
combined_iterator = itertools.chain(list1, list2, tuple1)
# Loop over the result
for item in combined_iterator:
print(item, end=" ") # Use end=" " to print on one line
The output will be a single, seamless sequence: 1 2 a b 10 20
itertools.count()
: The Infinite Counter
The itertools.count()
function creates an iterator that produces an endless sequence of evenly spaced numbers. It works just like range()
, but it has no stop
value—it will go on forever.
The syntax is: itertools.count(start=0, step=1)
import itertools
# Create a counter that starts at 10 and goes up by 2
counter = itertools.count(start=10, step=2)
# The built-in next() function gets the next item from an iterator
print(next(counter))
print(next(counter))
print(next(counter))
The output shows the lazy generation of each number:
10
12
14
Because this is a lazy iterator, creating an “infinite” counter is perfectly safe—it only ever produces a number when you ask for it.
itertools.islice()
: Slicing an Iterator
We know how to slice lists (e.g., my_list[1:5]
), but this syntax doesn’t work for generators or other iterators. This is where itertools.islice()
comes in.
It allows you to perform a lazy slice on any iterable, taking start
, stop
, and step
arguments, just like regular slicing. This is especially powerful when combined with an infinite iterator like count()
.
import itertools
# An infinite counter starting from 0
infinite_counter = itertools.count()
# Lazily get the items from index 5 up to (but not including) 10
sliced_items = itertools.islice(infinite_counter, 5, 10)
# Convert the iterator to a list to see the result
print(list(sliced_items))
The output will be: [5, 6, 7, 8, 9]
. The islice
function intelligently pulled just the values we wanted from the potentially infinite counter
.
What’s Next?
This has been just a small taste of what the itertools
module can do. It’s packed with many more incredibly useful functions for creating combinations, permutations, and other complex iteration patterns. When you have a complex looping problem, it’s always a good idea to check if itertools
already has an efficient solution for you.
We’re now going to pivot to one of the most powerful—and sometimes most confusing—features in Python. It’s a concept that combines many of the ideas we’ve learned: functions as objects, nested functions, and more. In Post #137, we will begin our multi-part look at understanding decorators.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com