{"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 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 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Itertools Module: 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. Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and …<\/p>\n\n
itertools.combinations_with_replacement(iterable, r)<\/pre>\n
Given string\u00a0 = \"hello\"\r\nGiven r value = 2<\/pre>\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
Given string\u00a0 = \"good\"\r\nGiven r value = 3<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import combinations_with_replacement() function from itertools module using the\r\n# import keyword.\r\nfrom itertools import combinations_with_replacement\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello\"\r\n# Give the r value as static input and store it in another variable.\r\ngvn_r_valu = 2\r\n# Pass the given string and 'r' value as the arguments to the\r\n# combinations_with_replacement() function and store it in a variable.\r\nrslt = combinations_with_replacement(gvn_str, gvn_r_valu)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(\"The possible combinations with repalcement are : \")\r\nprint(rslt_lst)\r\n<\/pre>\n
The possible combinations with repalcement 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
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import combinations_with_replacement() function from itertools module using the\r\n# import keyword.\r\nfrom itertools import combinations_with_replacement\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the r value as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_r_valu = int(input(\"Enter some random number = \"))\r\n# Pass the given string and 'r' value as the arguments to the\r\n# combinations_with_replacement() function and store it in a variable.\r\nrslt = combinations_with_replacement(gvn_str, gvn_r_valu)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(\"The possible combinations are : \")\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random string = good\r\nEnter some random number = 3\r\nThe possible combinations 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