itertools.combinations_with_replacement()
itertools.combinations_with_replacement() is a Python function in the Itertools Module category. Returns r-length combinations allowing individual elements to repeat. The syntax is itertools.combinations_with_replacement(iterable, r). Parameters: iterable, r. Returns: Iterator of tuples. A typical example: list(combinations_with_replacement([1,2], 2))
# [(1,1),(1,2),(2,2)]. A close sibling is itertools.chain(), which creates an iterator that returns elements from the first iterable until exhausted, then proceeds to the next. A close sibling is itertools.chain.from_iterable(), which alternative chain that takes a single iterable of iterables. A close sibling is itertools.product(), which cartesian product of input iterables (like nested for-loops). A close sibling is itertools.permutations(), which returns all possible r-length ordered arrangements of elements. More about this category: Iterator building blocks — chain, product, permutations, combinations, groupby. For a broader walkthrough, read the Itertools Module guide. Related Itertools Module entries: itertools.chain(), itertools.chain.from_iterable(), itertools.product(), itertools.permutations(), itertools.combinations(), itertools.cycle(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.