# pydoc > itertools

---
type: CommandReference
command: itertools
mode: pydoc
section: ''
source: pydoc3
---

## Quick Reference
- `count(start, step)` — infinite arithmetic progression
- `cycle(iterable)` — repeat elements indefinitely
- `repeat(object, times)` — repeat object, optionally limited
- `chain(*iterables)` — concatenate iterables
- `product(*iterables, repeat=1)` — Cartesian product
- `permutations(iterable, r)` — r-length permutations
- `combinations(iterable, r)` — r-length combinations
- `groupby(iterable, key)` — group consecutive elements by key

## Name
itertools — Functional tools for creating and using iterators.

## Synopsis
python
import itertools
## Options
### Infinite Iterators
- `count(start=0, step=1)` — Return consecutive integers starting from `start`, incrementing by `step`.
- `cycle(iterable)` — Cycle through elements of `iterable` indefinitely.
- `repeat(object [, times])` — Repeat `object`; if `times` is omitted, repeat endlessly.

### Iterators Terminating on Shortest Input
- `accumulate(iterable, func=None, *, initial=None)` — Return accumulated sums (or results of binary `func`); `initial` value can be specified.
- `chain(*iterables)` — Yield elements from first iterable, then second, etc.
- `chain.from_iterable(iterable)` — Alternative constructor; takes a single iterable of iterables, evaluated lazily.
- `compress(data, selectors)` — Return elements of `data` where the corresponding `selectors` element is true.
- `dropwhile(predicate, iterable)` — Drop items while `predicate(item)` is true, then yield the rest.
- `filterfalse(function, iterable)` — Yield items for which `function(item)` is false; if `function` is None, yield false items.
- `groupby(iterable, key=None)` — Yield consecutive keys and groups; `key` computes group category (defaults to identity).
- `islice(iterable, stop)` / `islice(iterable, start, stop[, step])` — Return selected elements from `iterable` similar to slicing.
- `pairwise(iterable)` — Yield successive overlapping pairs: (s0,s1), (s1,s2), ...
- `starmap(function, iterable)` — Yield `function(*item)` for each item in `iterable`.
- `takewhile(predicate, iterable)` — Yield items while `predicate(item)` is true.
- `zip_longest(*iterables, fillvalue=None)` — Zip iterables; fill missing values with `fillvalue` (default `None`).

### Combinatoric Generators
- `product(*iterables, repeat=1)` — Cartesian product; nested loops equivalent.
- `permutations(iterable, r=None)` — Successive r-length permutations; if `r` is None, length of `iterable`.
- `combinations(iterable, r)` — Successive r-length combinations in sorted order.
- `combinations_with_replacement(iterable, r)` — Successive r-length combinations allowing repeated elements.

### Functions
- `tee(iterable, n=2)` — Return a tuple of `n` independent iterators from a single `iterable`.

## Examples
python
# Infinite counting
for i in itertools.count(10, 2):
    if i > 20: break
    print(i)  # 10, 12, 14, 16, 18, 20

# Chaining iterables
list(itertools.chain('AB', 'CD'))  # ['A', 'B', 'C', 'D']

# Cartesian product
list(itertools.product('AB', range(2)))  # [('A',0), ('A',1), ('B',0), ('B',1)]

# Grouping by key
data = [('a', 1), ('a', 2), ('b', 3)]
for k, g in itertools.groupby(data, key=lambda x: x[0]):
    print(k, list(g))   # a [('a',1),('a',2)]  b [('b',3)]
## See Also
- [functools](https://docs.python.org/3/library/functools.html) — Higher-order functions and operations on callable objects.
- [operator](https://docs.python.org/3/library/operator.html) — Standard operators as functions.
- [collections](https://docs.python.org/3/library/collections.html) — Container datatypes.
- [more-itertools](https://pypi.org/project/more-itertools/) — Third-party library with additional itertools recipes.