Skip to Content
Course content

175: The itertools Module: chain, cycle, count

Click on the "Edit" button in the top corner of the screen to edit your slide content.

One thing I see developers do all the time when they first encounter itertools.chain is treat it as a convenient shortcut for adding lists together. They think, "Oh, I have three lists, I'll just chain them instead of using list1 + list2 + list3."

Chain isn't just a shortcut for the plus operator

If you're working with three lists of ten elements, using the + operator is fine. But here is why that mindset is dangerous: the plus operator creates a brand new list in memory. If you have three lists with a million elements each, list1 + list2 + list3 will allocate memory for a new list of three million elements before you even start processing the first one.

import itertools

# The "naive" way (Memory intensive)
big_list_a = range(1000000)
big_list_b = range(1000000)
combined = list(big_list_a) + list(big_list_b) # This creates a massive new list in RAM

# The itertools way (Memory efficient)
combined_iter = itertools.chain(big_list_a, big_list_b) # This creates an iterator
# No new massive list was created here. We just have a "pointer" to the sequences.

itertools.chain doesn't touch the data until you actually loop over it. It simply exhausts the first iterable, then seamlessly jumps to the second, and so on. It's an O(1) operation to set up, whereas list concatenation is O(N). In a production environment handling large datasets, this is the difference between a snappy application and a MemoryError crash.

Generating infinite IDs with count

Now, let's look at itertools.count(). You've probably written a while loop with a counter += 1 line a thousand times. It works, but it's boilerplate. I prefer count() when I need a unique, incrementing ID for objects as they arrive in a stream.

It's an infinite iterator. It will keep counting until your computer runs out of memory or you stop the loop. You can specify the start value and the step size.

import itertools

# Start at 100, increment by 1
id_generator = itertools.count(start=100)

for user in ["Alice", "Bob", "Charlie"]:
    print(f"User: {user}, ID: {next(id_generator)}")

# Output:
# User: Alice, ID: 100
# User: Bob, ID: 101
# User: Charlie, ID: 102

Handling rotation patterns with cycle

Finally, there's itertools.cycle(). I usually reach for this when I'm implementing something like a Round Robin scheduler. If you have a fixed set of resources—say, three database servers—and you want to distribute requests across them equally, you could use the modulo operator (index % 3). But that requires you to maintain an index integer manually.

cycle() removes that overhead by looping through an iterable forever.

import itertools

servers = ["Server-A", "Server-B", "Server-C"]
pool = itertools.cycle(servers)

# Imagine 7 incoming requests
for i in range(7):
    print(f"Request {i} routed to: {next(pool)}")

# Output:
# Request 0 routed to: Server-A
# Request 1 routed to: Server-B
# Request 2 routed to: Server-C
# Request 3 routed to: Server-A
# ... and so on.

The beauty here is that pool doesn't care how many requests come in. It just keeps spinning. Just a word of caution: never try to convert a cycle object into a list (e.g., list(pool)), or you'll hang your program in an infinite loop until it crashes.




📋 Practical Task

Build a Round-Robin Log Processor

You are tasked with building a log processing system. You have three different log files (represented as lists of strings) that need to be processed as one continuous stream. However, you need to assign a unique "Processing ID" to every single log entry, starting from 5000.

Your requirements:

  • Use itertools.chain to combine the three log lists without creating a new intermediate list.
  • Use itertools.count to generate the Processing IDs starting at 5000.
  • Print each log entry in the format: [ID] Log Message.
import itertools

log_web = ["Web: 200 OK", "Web: 404 Not Found"]
log_db = ["DB: Connection Timeout", "DB: Query Slow"]
log_auth = ["Auth: Login Success", "Auth: Password Reset"]

# YOUR CODE HERE:
# 1. Chain the three logs together.
# 2. Create a counter starting at 5000.
# 3. Loop through the chained logs and print them with their ID.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.