On this page
  1. itertools.cycle
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

itertools.cycle

Prev: itertools.count | Up: itertools | Next: itertools.dropwhile
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.cycle
Tags: #public #python3 #stdlib #itertools


Import

import itertools
from itertools import cycle

Signature & Overview

itertools.cycle(iterable, /)

Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.

Usage

from itertools import cycle, islice

colors = cycle(["red", "green", "blue"])
assert list(islice(colors, 5)) == ["red", "green", "blue", "red", "green"]

Gotchas

REPL Inspect

from itertools import cycle
help(cycle)

Linked from

Related notes