On this page
itertools.combinations_with_replacement
Prev: itertools.combinations | Up: itertools | Next: itertools.compress
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.combinations_with_replacement
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import combinations_with_replacement
Signature & Overview
itertools.combinations_with_replacement(iterable, r)
Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.
Usage
from itertools import combinations_with_replacement
assert list(combinations_with_replacement("AB", 2)) == [("A", "A"), ("A", "B"), ("B", "B")]
Gotchas
- Elements may repeat, order is ignored: Unlike
product(..., repeat=r),('B', 'A')is not emitted when('A', 'B')already was. - Position-based: Duplicate values in the input produce duplicate tuples in the output.
REPL Inspect
from itertools import combinations_with_replacement
help(combinations_with_replacement)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/accumulate Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/compress Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/count shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/dropwhile shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse shared tags: #itertools, #stdlib