0Pricing
Python Academy · Lesson

itertools: Infinite and Finite Iterators

Use count, cycle, islice, chain, and zip_longest.

itertools: Infinite and Finite Iterators is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is itertools?

itertools is a standard library module providing memory-efficient iterator building blocks. All functions return iterators, not lists.

import itertools

# count: infinite counter
for i in itertools.islice(itertools.count(10, 2), 5):
    print(i, end=" ")  # 10 12 14 16 18

count()

count(start, step) produces an infinite arithmetic sequence. Always pair with islice or a break condition.

import itertools

for n in itertools.count(1, 10):
    if n > 40:
        break
    print(n, end=" ")  # 1 11 21 31 41

cycle()

cycle(iterable) repeats the iterable infinitely.

import itertools

colors = itertools.cycle(["red","green","blue"])
for _ in range(7):
    print(next(colors), end=" ")
# red green blue red green blue red

repeat()

repeat(value, times) yields the same value times times (infinite if omitted).

import itertools
print(list(itertools.repeat(42, 4)))   # [42, 42, 42, 42]
print(list(map(pow, range(5), itertools.repeat(2))))
# [0, 1, 4, 9, 16]

islice()

islice(it, stop) or islice(it, start, stop, step) slices an iterator without materialising it.

import itertools

result = list(itertools.islice(range(100), 2, 10, 2))
print(result)  # [2, 4, 6, 8]

chain()

chain(*iterables) chains multiple iterables end-to-end into one iterator.

import itertools

result = list(itertools.chain([1,2], [3,4], [5]))
print(result)  # [1, 2, 3, 4, 5]

# chain.from_iterable for a list of iterables:
nested = [[1,2],[3,4]]
print(list(itertools.chain.from_iterable(nested)))

zip_longest()

zip_longest zips iterables of unequal length, filling missing values with a fillvalue.

import itertools

a = [1, 2, 3]
b = ["a", "b"]
print(list(itertools.zip_longest(a, b, fillvalue=0)))
# [(1, "a"), (2, "b"), (3, 0)]

dropwhile() and takewhile()

takewhile(pred, it) yields items while the predicate is true. dropwhile does the opposite.

import itertools

nums = [1, 3, 5, 6, 7, 9]
print(list(itertools.takewhile(lambda x: x % 2, nums)))
# [1, 3, 5]

print(list(itertools.dropwhile(lambda x: x % 2, nums)))
# [6, 7, 9]

filterfalse()

filterfalse(pred, it) yields elements where the predicate is False — the complement of filter().

import itertools

evens = list(itertools.filterfalse(lambda x: x%2, range(10)))
print(evens)  # [0, 2, 4, 6, 8]

starmap()

starmap(func, iterable) applies a function to each tuple of arguments from an iterable.

import itertools, operator

pairs = [(2,3),(4,5),(1,10)]
result = list(itertools.starmap(operator.mul, pairs))
print(result)  # [6, 20, 10]

groupby()

groupby(it, key) groups consecutive elements with the same key value. Sort the iterable first for correct grouping.

import itertools

data = sorted([("a",1),("b",2),("a",3)], key=lambda x:x[0])
for key, group in itertools.groupby(data, key=lambda x:x[0]):
    print(key, list(group))

Quick Check

What does itertools.chain([1,2], [3,4]) produce?

Recap

itertools provides infinite iterators (count, cycle, repeat), slicing (islice), combination (chain, zip_longest), and filtering (takewhile, dropwhile, filterfalse). All are lazy and memory-efficient.

Frequently asked questions

Is the “itertools: Infinite and Finite Iterators” lesson free?

Yes — the full text of “itertools: Infinite and Finite Iterators” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “itertools: Infinite and Finite Iterators”?

Use count, cycle, islice, chain, and zip_longest. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “itertools: Infinite and Finite Iterators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. itertools: Infinite and Finite Iterators
  2. itertools: Combinatorics
  3. functools: partial and reduce
  4. functools: lru_cache and cached_property
← Back to Python Academy