{"id":7339,"date":"2020-07-24T13:53:11","date_gmt":"2020-07-24T13:53:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7339"},"modified":"2020-07-24T13:53:13","modified_gmt":"2020-07-24T13:53:13","slug":"permutations-and-combinations-using-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/permutations-and-combinations-using-python","title":{"rendered":"Permutations and Combinations using Python"},"content":{"rendered":"\n<p>In this article, we will be learning how to find permutations and combinations using Python. <\/p>\n\n\n\n<p>Python provides a library named <a href=\"https:\/\/www.askpython.com\/python-modules\/python-itertools-module\" class=\"rank-math-link\">itertools<\/a> that contains in-built functions to calculate permutations and combinations. Let us quickly look at the implementation of these functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importing the required library<\/h2>\n\n\n\n<p>Before we are able to use any of the following functions, we need to import the <code>itertools<\/code> library. It is done by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n<\/pre><\/div>\n\n\n<p>The above statement imports the library and forms a pathway to use its functions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Finding Permutations<\/h2>\n\n\n\n<p>Permutation mathematically refers to <em>&#8220;the arrangement of certain numbers or letters&#8221;<\/em>. The <code>permutations()<\/code> function in the <code>itertools<\/code> library does exactly that. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Permutations of a Python string<\/h3>\n\n\n\n<p>If we are given a <a href=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\" class=\"rank-math-link\">Python string<\/a> and asked to find out all the ways its letters can be arranged, then the task can easily be achieved by the <code>permutations()<\/code> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nst = &quot;ABC&quot;\n\nper = itertools.permutations(st)\n\nfor val in per:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\nA B C\nA C B\nB A C\nB C A\nC A B\nC B A\n<\/pre><\/div>\n\n\n<p>The function <code>permutations()<\/code> takes a String argument, and provides an <code>itertools<\/code> object. In case we try to print the variable <code>'per'<\/code> directly, we will get the following:  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\n&lt;itertools.permutations object at 0x7fc9abdd8308&gt;\n<\/pre><\/div>\n\n\n<p>Therefore it is a necessity to run a loop to print each entry. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Permutations of multiple numbers<\/h3>\n\n\n\n<p>The <code>permuatations()<\/code> function takes an iterable argument, therefore in order to find out permutations of numbers, we need to pass the numbers as a list, set, or <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">tuple<\/a>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nvalues = &#x5B;1, 2, 3]\n\nper = itertools.permutations(values)\n\nfor val in per:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 2 3\n1 3 2\n2 1 3\n2 3 1\n3 1 2\n3 2 1\n<\/pre><\/div>\n\n\n<p>In the above techniques to calculate permutations, we are including all the numbers or letters. There is a possibility to restrict the number of elements in the permutations. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Permutations with certain number of elements<\/h3>\n\n\n\n<p>Similar, to the concept of <strong>&#8216;nPr&#8217;<\/strong>, which states <em>&#8220;Arranging r elements out of n&#8221;<\/em>, this can be achieved by passing an integer after the set of elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nvalues = &#x5B;1, 2, 3, 4]\n\nper = itertools.permutations(values, 2)\n\nfor val in per:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3\n<\/pre><\/div>\n\n\n<p>In the above code snippet, the <code>permutations()<\/code> function is asked to arrange only 2 elements at a time from the list of numbers provided.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Finding Combinations<\/h2>\n\n\n\n<p>The term Combinations, refer to the ways of picking up elements from a set of objects. The <code>itertools<\/code> library provides a method <code>combinations()<\/code> for exactly this functionality.<\/p>\n\n\n\n<p>One thing to note here is that, picking a set of objects does not involve arranging. The <code>combinations()<\/code> function takes two arguments: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>the set of values<\/li><li>an integer denoting the number of values to be picked for a combination.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">1. Combinations for letters in a word<\/h3>\n\n\n\n<p>Given a word, if we need to find all combinations containing exactly 2 letters from the word, then <code>combinations()<\/code> is the go-to function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nst = &quot;ABCDE&quot;\n\ncom = itertools.combinations(st, 2)\n\nfor val in com:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\nA B\nA C\nA D\nA E\nB C\nB D\nB E\nC D\nC E\nD E\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Combinations of set of numbers<\/h3>\n\n\n\n<p>Similar to the combinations result we got for letters in a word, it can be achieved for numbers in a list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nvalues = &#x5B;1, 2, 3, 4]\n\ncom = itertools.combinations(values, 2)\n\nfor val in com:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong> The combinations generated are based on the index values of each object, not their actual values, therefore in case any value is repeated, then the function prints similar combinations thinking each value is different.<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Combinations for repeated numbers<\/h3>\n\n\n\n<p>To further explain the above <strong>Note<\/strong>, let us run an example for it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nvalues = &#x5B;1, 1, 2, 2]\n\ncom = itertools.combinations(values, 2)\n\nfor val in com:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 1\n1 2\n1 2\n1 2\n1 2\n2 2\n<\/pre><\/div>\n\n\n<p>The result is self-explanatory, as the numbers in the list are repeated.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-very-dark-gray-background-color has-very-dark-gray-color is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Combinations of numbers with itself<\/h3>\n\n\n\n<p>There is yet another function related to permutations and combinations in the <code>itertools<\/code> library called <code>combinations_with_replacement()<\/code>. This function is a variation of <code>combinations()<\/code> function, with a slight difference that it includes combinations of elements with themselves.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport itertools\n\nvalues = &#x5B;1, 2, 3, 4]\n\ncom = itertools.combinations_with_replacement(values, 2)\n\nfor val in com:\n\tprint(*val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 1\n1 2\n1 3\n1 4\n2 2\n2 3\n2 4\n3 3\n3 4\n4 4\n<\/pre><\/div>\n\n\n<p>We can see the clear distinction of the above output from the output with the same parameters when passed to <code>combinations()<\/code> function.<\/p>\n\n\n\n<p>There are combinations of numbers with themselves, like there are multiple instances of them in the list. This basically happens because, when we pick an element from the list, the above function, places the same value again, in order to get combinations with itself.<\/p>\n\n\n\n<p>This sums the topic of permutations and combinations using Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The application of permutations and combinations are vast in the field of mathematics. The methods explained in this tutorial come handy while solving problems regarding such mathematical techniques.<\/p>\n\n\n\n<p>We hope this article for easy to follow. Feel free to comment below for queries and suggestions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be learning how to find permutations and combinations using Python. Python provides a library named itertools that contains in-built functions to calculate permutations and combinations. Let us quickly look at the implementation of these functions. Importing the required library Before we are able to use any of the following functions, [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":7353,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7339"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7339\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7353"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}