{"id":25602,"date":"2021-11-23T08:53:51","date_gmt":"2021-11-23T03:23:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=25602"},"modified":"2021-11-23T08:53:51","modified_gmt":"2021-11-23T03:23:51","slug":"python-itertools-combinations_with_replacement-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-combinations_with_replacement-function-with-examples\/","title":{"rendered":"Python Itertools.combinations_with_replacement() Function with Examples"},"content":{"rendered":"

Itertools Module:<\/strong><\/p>\n

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.<\/p>\n

Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and space efficiency. Itertools enables us to solve complex problems quickly and easily. Iterators are classified into three types.<\/p>\n

This module provides the following types of iterators:<\/p>\n

    \n
  1. Combinatorics Generators<\/li>\n
  2. Infinite Iterators<\/li>\n
  3. Terminating Iterators<\/li>\n<\/ol>\n

    itertools.combinations_with_replacement() Function:<\/strong><\/p>\n

    itertools.combinations_with_replacement() function belongs to the itertools Combinatoric Generator subtype. Combinatoric generators are iterators that deal with the various arrangements that an iterator can take. The elements are referred to here by their index value rather than their value or type.<\/p>\n

    Usage of this function<\/strong><\/p>\n

    The term “combinations” refers to all of the possible subsets or arrangements of the iterator, whereas “combinations with replacement” refers to all of the possible arrangements or subsets that allow an element to repeat in a subset.<\/p>\n

    This function accepts ‘r’ as an input, where ‘r’ represents the number of possible combinations. All combinations with element repetition are emitted and have a length of ‘r,’ and ‘r’ is a necessary argument here.<\/p>\n

    Syntax:<\/strong><\/p>\n

    itertools.combinations_with_replacement(iterable, r)<\/pre>\n

    Parameters<\/strong><\/p>\n

    iterable:<\/strong> This is Required. Elements whose combinations must be performed out.<\/p>\n

    r:<\/strong> This is Required. It is the length of the outputs.<\/p>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    Given string\u00a0 = \"hello\"\r\nGiven r value =  2<\/pre>\n

    Output:<\/strong><\/p>\n

    The possible combinations with replacemenet are : \r\n[('h', 'h'), ('h', 'e'), ('h', 'l'), ('h', 'l'), ('h', 'o'), ('e', 'e'), ('e', 'l'), ('e', 'l'), ('e', 'o'), ('l', 'l'), ('l', 'l'), ('l', 'o'), ('l', 'l'), ('l', 'o'), ('o', 'o')]<\/pre>\n

    Example2:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    Given string\u00a0 = \"good\"\r\nGiven r value = 3<\/pre>\n

    Output:<\/strong><\/p>\n

    The possible combinations with replacemenet are : \r\n[('g', 'g', 'g'), ('g', 'g', 'o'), ('g', 'g', 'o'), ('g', 'g', 'd'), ('g', 'o', 'o'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('d', 'd', 'd')]<\/pre>\n

    itertools.combinations_with_replacement() Function with Examples in Python<\/h2>\n