Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. itertools.chain() / itertools.islice() / itertools.count()

itertools.chain() / itertools.islice() / itertools.count()

The itertools module provides tools for working with iterators. itertools.chain() concatenates multiple iterables and lets you treat them as a single iterator. itertools.islice() slices an iterator, and itertools.count() generates numbers indefinitely. These functions operate with lazy evaluation, so they can process large amounts of data with minimal memory usage.

Syntax

import itertools

# Concatenate multiple iterables
itertools.chain(iterable1, iterable2, ...)

# Slice an iterator
itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop, step)

# Infinite counter
itertools.count(start=0, step=1)

# Infinite cycle
itertools.cycle(iterable)

# Repeat an element
itertools.repeat(object, times=None)

Function List

FunctionDescription
chain(*iterables)Concatenates multiple iterables into a single iterator.
chain.from_iterable(iterable)Flattens a nested iterable by one level.
islice(iterable, stop)Slices an iterator and returns elements within the specified range.
count(start, step)Generates an infinite sequence starting at start, incrementing by step.
cycle(iterable)Repeats the elements of an iterable indefinitely.
repeat(obj, times)Returns an iterator that repeats obj for times iterations, or indefinitely if omitted.

Sample Code

import itertools

# chain: concatenate multiple lists
a = [1, 2, 3]
b = [4, 5, 6]
c = ['x', 'y']
result = list(itertools.chain(a, b, c))
print(result)   # [1, 2, 3, 4, 5, 6, 'x', 'y']

# chain.from_iterable: flatten a nested list
nested = [[1, 2], [3, 4], [5, 6]]
flat = list(itertools.chain.from_iterable(nested))
print(flat)     # [1, 2, 3, 4, 5, 6]

# islice: safely consume an infinite iterator
counter = itertools.count(10, 2)    # 10, 12, 14, ...
sliced = list(itertools.islice(counter, 5))
print(sliced)   # [10, 12, 14, 16, 18]

# islice: specify start, stop, and step
data = range(100)
result = list(itertools.islice(data, 0, 20, 3))
print(result)   # [0, 3, 6, 9, 12, 15, 18]

# cycle: generate a repeating pattern
colors = ['red', 'blue', 'green']
cycled = list(itertools.islice(itertools.cycle(colors), 7))
print(cycled)   # ['red', 'blue', 'green', 'red', 'blue', 'green', 'red']

# repeat: repeat the same value n times
repeated = list(itertools.repeat('hello', 3))
print(repeated)  # ['hello', 'hello', 'hello']

# combine with map() to generate a list of powers
squares = list(map(pow, range(5), itertools.repeat(2)))
print(squares)   # [0, 1, 4, 9, 16]

Notes

chain() is more efficient than using the + operator to join lists, because it does not copy elements — it links the iterators together and returns them lazily. This makes it especially useful when combining a large number of lists.

islice() is commonly used alongside infinite iterators. Since standard slice syntax ([start:stop]) does not work on iterators, islice() serves as the alternative.

count(), cycle(), and repeat() generate elements indefinitely, so always pair them with islice() or a conditional break. They can also be combined with zip() to generate numbered sequences or repeating-pattern datasets.

If you find any errors or copyright issues, please .