Python Glossary Русский

all Python 3operatorfunction

operator.concat

sequences

Concatenates two sequences: a + b (lists, strings, tuples).

Syntax

operator.concat(a, b)

It is stricter than add: both arguments must be sequences, so concat(1, 2) raises TypeError where add(1, 2) would return 3. The types must match: a list will not concatenate with a tuple. Repeated concat via reduce is slow because every step copies everything so far. To join many strings use str.join; many lists, itertools.chain.

Examples

import operator
from functools import reduce
print(operator.concat([1], [2]))   # → [1, 2]
print(operator.concat('py', 'thon'))   # → python
print(operator.concat((1, 2), (3,)))   # → (1, 2, 3)
print(reduce(operator.concat, [[1], [2], [3]]))   # → [1, 2, 3]
print(operator.concat(1, 2))   # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy