

{"id":10483,"date":"2018-03-12T08:38:43","date_gmt":"2018-03-12T03:08:43","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=10483"},"modified":"2026-04-28T14:47:36","modified_gmt":"2026-04-28T09:17:36","slug":"python-itertools-tutorial","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/","title":{"rendered":"Python Itertools &#8211; A Quick and Easy Guide"},"content":{"rendered":"<p>In our write-up on Python Iterables, we took a brief introduction to the Python itertools module. This is what will be the point of focus in today&#8217;s Python itertools tutorial.<\/p>\n<p>Here, we will learn how to get infinite iterators &amp; Combinatoric Iterators by Python Itertools.<\/p>\n<p>Along with this, we will learn how Python Iterators terminate the shortest input sequence.<\/p>\n<p>So, let&#8217;s start exploring Python Itertools Tutorial.<\/p>\n<h3>What are Python Itertools?<\/h3>\n<p>The Python itertools module has functions for creating iterators for efficient looping. While some iterators are infinite, some terminate on the shortest input sequence. Yet, some are combinatoric.<\/p>\n<p><strong>Benefits of using itertools in Python:<\/strong><\/p>\n<ul>\n<li><strong>Saves memory:<\/strong> The list doesn&#8217;t create a huge list altogether; it collects items one by one, which helps in avoiding crashes.<\/li>\n<li><strong>Fast:<\/strong> Python uses the C language, which is faster than writing your own loop.<\/li>\n<li><strong>Clearer code:<\/strong> it converts messy code into clear lines.<\/li>\n<\/ul>\n<p>Let\u2019s first discuss Python infinite iterators.<\/p>\n<h3>Infinite Iterators in Python<\/h3>\n<p>Some functions are capable of generating infinite iterators. In this Python Itertools tutorial, we will study the following functions:<\/p>\n<h4>1. count([start=0, step=1]) in Python<\/h4>\n<p>count() may take two values- start and step. It then returns a sequence of values from start, with intervals the size of step.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import count\r\n&gt;&gt;&gt; for i in count(10,2):\r\n                print(i)\r\n                if i&gt;25: break<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">10<br \/>\n12<br \/>\n14<br \/>\n16<br \/>\n18<br \/>\n20<br \/>\n22<br \/>\n24<br \/>\n26<\/div>\n<p>Here\u2019s count() with one argument:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in count(2):\r\n               print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\nTraceback (most recent call last):File &#8220;&lt;pyshell#166&gt;&#8221;, line 2, in &lt;module&gt;<br \/>\nprint(i)<br \/>\nKeyboardInterrupt<\/div>\n<p>It takes a step of 1. If we call it without an argument, it starts with 0:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in count():\r\n                print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\n10<br \/>\n11<br \/>\n12<br \/>\n13<br \/>\n14<br \/>\n15<br \/>\nTraceback (most recent call last):File &#8220;&lt;pyshell#169&gt;&#8221;, line 2, in &lt;module&gt;<br \/>\nprint(i)<br \/>\nKeyboardInterrupt<\/div>\n<h4>2. cycle(iterable) in Python<\/h4>\n<p>cycle() makes an iterator from elements from an iterable, and saves a copy of each.<\/p>\n<p>Once exhausted, it returns elements from that copy. This repeats indefinitely.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import cycle\r\n&gt;&gt;&gt; for i in cycle(['red','green','blue']):\r\n                print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">red<br \/>\ngreen<br \/>\nblue<br \/>\nred<br \/>\ngreen<br \/>\nblue<br \/>\nred<br \/>\ngreen<br \/>\nblue<br \/>\nred<br \/>\ngreen<br \/>\nblue<br \/>\nred<br \/>\ngreen<br \/>\nblue<br \/>\nTraceback (most recent call last):File &#8220;&lt;pyshell#174&gt;&#8221;, line 2, in &lt;module&gt;<br \/>\nprint(i)<br \/>\n&lt;KeyboardInterrupt<\/div>\n<pre class=\"EnlighterJSRAW\"><\/pre>\n<p>cycle() can take any kind of iterable.<\/p>\n<h4>3. repeat(elem [,n]) in Python<\/h4>\n<p>This will repeat element elem n-times or endlessly into the iterator.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import repeat\r\n&gt;&gt;&gt; for i in repeat(\u2018Red\u2019,3):\r\n                print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Red<br \/>\nRed<br \/>\nRed<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for  i in repeat('Red'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Red<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nRed<br \/>\nTraceback (most recent call last):File &#8220;&lt;pyshell#181&gt;&#8221;, line 2, in &lt;module&gt;<br \/>\nprint(i)<br \/>\nKeyboardInterrupt<\/div>\n<h3>Combinatoric Iterators in Python<\/h3>\n<p>Combinatorial pertains to the arrangement of, operation on, and selection of discrete mathematical elements. Let\u2019s now talk about combinatoric iterators.<\/p>\n<h4>1. product(*iterables, repeat=1) in Python<\/h4>\n<p>product() returns the Cartesian product of the input iterables. This is equivalent to a nested for-loop.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in product([1,2,3],[4,5,6]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 4)<br \/>\n(1, 5)<br \/>\n(1, 6)<br \/>\n(2, 4)<br \/>\n(2, 5)<br \/>\n(2, 6)<br \/>\n(3, 4)<br \/>\n(3, 5)<br \/>\n(3, 6)<\/div>\n<p>This is equivalent to iterating over this generator object:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in ((i,j) for i in [1,2,3] for j in [4,5,6]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(1, 4)<br \/>\n(1, 5)<br \/>\n(1, 6)<br \/>\n(2, 4)<br \/>\n(2, 5)<br \/>\n(2, 6)<br \/>\n(3, 4)<br \/>\n(3, 5)<br \/>\n(3, 6)<\/div>\n<p>Let\u2019s take a couple more examples.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in product('AB','CD','EF'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;E&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;F&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;E&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;F&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;E&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;F&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;E&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;F&#8217;)<\/div>\n<p>Because of the repeat argument, the rightmost element advances with every iteration.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in product('AB','CD',repeat=2):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<\/div>\n<h4>2. permutations(iterable,r=None) in Python<\/h4>\n<p>permutations() returns r-length permutations of elements in the iterable.<\/p>\n<p>It generates all possible permutations in lexicographic order, and there is no repetition of elements.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import permutations\r\n&gt;&gt;&gt; for i in permutations('ABCD'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;B&#8217;, &#8216;D&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;D&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;C&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;A&#8217;, &#8216;C&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;A&#8217;, &#8216;D&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;D&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;C&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;A&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;A&#8217;, &#8216;D&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;B&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;B&#8217;, &#8216;D&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;, &#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;, &#8216;B&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;A&#8217;, &#8216;C&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;B&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;B&#8217;, &#8216;C&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;C&#8217;, &#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;C&#8217;, &#8216;B&#8217;, &#8216;A&#8217;)<\/div>\n<p>Here, we didn\u2019t pass a second argument to it, so it printed tuples of length 4, which is the length of the iterable.<\/p>\n<p>Now, let\u2019s pass a value of 3 to this code.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in permutations('ABCD',3):\r\n                print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;B&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;B&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;C&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;C&#8217;, &#8216;B&#8217;)<\/div>\n<h4>3. combinations(iterable,r) in Python<\/h4>\n<p>This returns subsequences of length r from the elements of the iterable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import combinations\r\n&gt;&gt;&gt; for i in combinations('ABCD',2):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;)<\/div>\n<p>If you noticed, this only returns the tuples that are lexicographically ascending.<\/p>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in combinations(range(4),3):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(0, 1, 2)<br \/>\n(0, 1, 3)<br \/>\n(0, 2, 3)<br \/>\n(1, 2, 3)<\/div>\n<h4>4. combinations_with_replacement(iterable, r) in Python<\/h4>\n<p>This returns r-length subsequences of elements of the iterable, where individual elements may repeat.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import combinations_with_replacement as cwr\r\n&gt;&gt;&gt; for i in cwr('ABCD',2):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;A&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;A&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;B&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;C&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;D&#8217;)<br \/>\n(&#8216;D&#8217;, &#8216;D&#8217;)<\/div>\n<h3>Iterators Terminating on the Shortest Input Sequence<\/h3>\n<h4>1. accumulate(iterable [,func]) in Python<\/h4>\n<p>This makes an iterator with accumulated sums (or accumulated results of a binary function specified).<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import accumulate\r\n&gt;&gt;&gt; for i in accumulate([0,1,0,1,1,2,3,5]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n5<br \/>\n8<br \/>\n13<\/div>\n<p>This prints the Fibonacci series.<\/p>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import operator\r\n&gt;&gt;&gt; for i in accumulate([1,2,3,4,5],operator.mul):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n6<br \/>\n24<br \/>\n120<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in accumulate([2,1,4,3,5],max):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n2<br \/>\n4<br \/>\n4<br \/>\n5<\/div>\n<h4>2. chain(*iterables) in Python<\/h4>\n<p>chain() makes an iterator from elements of the first iterable, then from the second, and so on.<\/p>\n<p>It moves to the next iterable as one iterable exhausts.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import chain\r\n&gt;&gt;&gt; for i in chain('Hello','World','Bye'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">H<br \/>\ne<br \/>\nl<br \/>\nl<br \/>\no<br \/>\nW<br \/>\no<br \/>\nr<br \/>\nl<br \/>\nd<br \/>\nB<br \/>\ny<br \/>\ne<\/div>\n<h4>3. chain.from_iterable(iterable) in Python<\/h4>\n<p>This is an alternative constructor to chain(). It takes chained inputs from a single iterable argument and evaluates them lazily.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in chain.from_iterable(['Hello','World','Bye']):\r\n               print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">H<br \/>\ne<br \/>\nl<br \/>\nl<br \/>\no<br \/>\nW<br \/>\no<br \/>\nr<br \/>\nl<br \/>\nd<br \/>\nB<br \/>\ny<br \/>\ne<\/div>\n<p>You don\u2019t need to additionally import anything for this.<\/p>\n<h4>4. compress(data, selectors) in Python<\/h4>\n<p>This makes an iterator that filters elements, from data, for which selector values amount to True.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import compress\r\n&gt;&gt;&gt; for i in compress('ABCDEF',[1,0,1,True,0,' ']):\r\n              print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">A<br \/>\nC<br \/>\nD<br \/>\nF<\/div>\n<h4>5. dropwhile(predicate,iterable) in Python<\/h4>\n<p>As long as the predicate is True, it drops elements from the iterable. As soon as it is False, it starts returning every element.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import dropwhile\r\n&gt;&gt;&gt; for i in dropwhile(lambda x:x&lt;7,[1,2,7,9,5,3,2,9]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<br \/>\n9<br \/>\n5<br \/>\n3<br \/>\n2<br \/>\n9<\/div>\n<h4>6. filterfalse(predicate,iterable) in Python<\/h4>\n<p>This makes an iterator that filters those elements out from the iterator, for which the predicate is True.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import filterfalse\r\n&gt;&gt;&gt; for i in filterfalse(lambda x:x&lt;7,[1,2,7,9,5,3,2,9]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<br \/>\n9<br \/>\n9<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in filterfalse(lambda x:x%2,[1,2,7,9,5,3,2,9]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n2<\/div>\n<h4>7. groupby(iterable,key=None) in Python<\/h4>\n<p>This makes an iterator that takes the iterable, and returns consecutive keys and groups. These are sub-iterators grouped by the key.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import groupby\r\n&gt;&gt;&gt; for i,j in groupby('AAAAABBCCCCCDDDCCCBBA'):\r\n                print(list(j))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;A&#8217;, &#8216;A&#8217;, &#8216;A&#8217;, &#8216;A&#8217;, &#8216;A&#8217;]<br \/>\n[&#8216;B&#8217;, &#8216;B&#8217;]<br \/>\n[&#8216;C&#8217;, &#8216;C&#8217;, &#8216;C&#8217;, &#8216;C&#8217;, &#8216;C&#8217;]<br \/>\n[&#8216;D&#8217;, &#8216;D&#8217;, &#8216;D&#8217;]<br \/>\n[&#8216;C&#8217;, &#8216;C&#8217;, &#8216;C&#8217;]<br \/>\n[&#8216;B&#8217;, &#8216;B&#8217;]<br \/>\n[&#8216;A&#8217;]<\/div>\n<h4>8. islice(iterable,stop) in Python<\/h4>\n<p>islice(iterable,start,stop [,step])<\/p>\n<p>This makes an iterator that returns selected elements from the iterable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import islice\r\n&gt;&gt;&gt; for i in islice([1,2,3,4,5],2):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in islice([1,2,3,4,5],2,5):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n4<br \/>\n5<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in islice([1,2,3,4,5],0,5,2):\r\n                print(i)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n3<br \/>\n5<\/div>\n<h4>9. starmap(function,iterable) in Python<\/h4>\n<p>This makes an iterator that takes arguments from the iterable and computes a function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import starmap\r\n&gt;&gt;&gt; for i in starmap(operator.sub,[(2,1),(7,3),(15,10)]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n4<br \/>\n5<\/div>\n<h4>10. takewhile(predicate,iterable) in Python<\/h4>\n<p>This makes an iterator that returns elements from the iterator as long as the predicate amounts to True. This is in contrast to dropwhile().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import takewhile\r\n&gt;&gt;&gt; for i in takewhile(lambda x:x&lt;7,[1,2,7,9,5,3,2,9]):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<\/div>\n<h4>11. tee(n=2) in Python<\/h4>\n<p>This splits an iterator into n independent iterators.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import tee\r\n&gt;&gt;&gt; for i in tee([1,2,3,4,5,6,7],3):\r\n                for j in i: print(j)\r\n                print()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n71<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n71<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<\/div>\n<h4>12. zip_longest(*iterables,fillvalue=None) in Python<\/h4>\n<p>This makes an iterator by aggregating elements from each iterable. The fillvalue parameter tells it a value to fill for the remaining places in the shorter iterable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from itertools import zip_longest\r\n&gt;&gt;&gt; for i in zip_longest('ABC','12345',fillvalue='*'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;1&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;2&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;3&#8217;)<br \/>\n(&#8216;*&#8217;, &#8216;4&#8217;)<br \/>\n(&#8216;*&#8217;, &#8216;5&#8217;)<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in zip_longest('ABC','12345','Hello',fillvalue='*'):\r\n                print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;A&#8217;, &#8216;1&#8217;, &#8216;H&#8217;)<br \/>\n(&#8216;B&#8217;, &#8216;2&#8217;, &#8216;e&#8217;)<br \/>\n(&#8216;C&#8217;, &#8216;3&#8217;, &#8216;l&#8217;)<br \/>\n(&#8216;*&#8217;, &#8216;4&#8217;, &#8216;l&#8217;)<br \/>\n(&#8216;*&#8217;, &#8216;5&#8217;, &#8216;o&#8217;)<\/div>\n<p>So, this was all about the Python Itertools Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Itertools<\/h3>\n<p>1. What are itertools in Python?<\/p>\n<p>2. What does itertools do in Python?<\/p>\n<p>3. Is itertool a standard library in Python?<\/p>\n<p>4. What are the benefits and limitations of using itertools in Python?<\/p>\n<p>5. How does itertools work in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>Processing big data often means iterating over millions of records. The itertools module supplies memory-friendly \u201clazy\u201d iterators that yield items one by one instead of building giant lists. itertools.count() creates an infinite counter, cycle() repeats a pattern, and repeat(x, n) stamps out a value multiple times\u2014without heavy RAM costs.<\/p>\n<p>Hope you enjoyed the Python itertools tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our write-up on Python Iterables, we took a brief introduction to the Python itertools module. This is what will be the point of focus in today&#8217;s Python itertools tutorial. Here, we will learn&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":36198,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2653,6697,7340,10622,10625,10626],"class_list":["post-10483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-combinatoric-iterators","tag-infinite-iterators","tag-iterators-terminating-on-the-shortest-input-sequence","tag-python-iterables","tag-python-iterators","tag-python-itertools"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Itertools - A Quick and Easy Guide - DataFlair<\/title>\n<meta name=\"description\" content=\"Python Itertools Tutorial:What is Python Iteratools, Learn Infinite Iterators, Combinatoric Iterators, Iterators Terminating on the Shortest Input Sequence\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Itertools - A Quick and Easy Guide - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python Itertools Tutorial:What is Python Iteratools, Learn Infinite Iterators, Combinatoric Iterators, Iterators Terminating on the Shortest Input Sequence\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-12T03:08:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:17:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Itertools - A Quick and Easy Guide - DataFlair","description":"Python Itertools Tutorial:What is Python Iteratools, Learn Infinite Iterators, Combinatoric Iterators, Iterators Terminating on the Shortest Input Sequence","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python Itertools - A Quick and Easy Guide - DataFlair","og_description":"Python Itertools Tutorial:What is Python Iteratools, Learn Infinite Iterators, Combinatoric Iterators, Iterators Terminating on the Shortest Input Sequence","og_url":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-03-12T03:08:43+00:00","article_modified_time":"2026-04-28T09:17:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Itertools &#8211; A Quick and Easy Guide","datePublished":"2018-03-12T03:08:43+00:00","dateModified":"2026-04-28T09:17:36+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/"},"wordCount":1400,"commentCount":5,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg","keywords":["Combinatoric Iterators","Infinite Iterators","Iterators Terminating on the Shortest Input Sequence","python iterables","Python iterators","Python Itertools"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/","url":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/","name":"Python Itertools - A Quick and Easy Guide - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg","datePublished":"2018-03-12T03:08:43+00:00","dateModified":"2026-04-28T09:17:36+00:00","description":"Python Itertools Tutorial:What is Python Iteratools, Learn Infinite Iterators, Combinatoric Iterators, Iterators Terminating on the Shortest Input Sequence","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Python-itertools-01-1-1.jpg","width":1200,"height":628,"caption":"Python Itertools Tutorial - A Quick and Easy Guide"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-itertools-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Itertools &#8211; A Quick and Easy Guide"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/10483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=10483"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/10483\/revisions"}],"predecessor-version":[{"id":148015,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/10483\/revisions\/148015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/36198"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=10483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=10483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=10483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}