Python Glossary Русский

all Python 3itertoolsfunction

itertools.starmap

itertools

Like map(), but arguments are taken by unpacking each tuple: f(*args).

Syntax

itertools.starmap(function, iterable)

Reach for starmap when the arguments already come grouped as tuples inside one iterable; use plain map(f, a, b) when the iterables are separate. It is essentially (f(*args) for args in seq).

Examples

import itertools
print(list(itertools.starmap(pow, [(2, 3), (3, 2)])))   # → [8, 9]
print(list(itertools.starmap(max, [(1, 5), (9, 2), (4, 4)])))   # → [5, 9, 4]
print(list(itertools.starmap(lambda n, s: f'{n}: {s}', [('Ann', 90), ('Bob', 75)])))   # → ['Ann: 90', 'Bob: 75']
print(list(map(pow, [2, 3], [3, 2])))   # → [8, 9]
print(list(itertools.starmap(pow, [(2,)])))   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy