

{"id":6731,"date":"2018-01-29T12:54:30","date_gmt":"2018-01-29T07:24:30","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6731"},"modified":"2026-04-28T14:40:29","modified_gmt":"2026-04-28T09:10:29","slug":"python-iterator","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-iterator\/","title":{"rendered":"What is Python Iterator &#8211; Create your own Iterator"},"content":{"rendered":"<p>In this Python Iterator Tutorial, we will learn what a Python iterator.<\/p>\n<p>We will also discuss how to create our own __iter__() and __next__() methods, building a Python iterator, a for loop in Python iterator, an infinite Python iterator, and the benefits of an iterator in Python with an example.<\/p>\n<p>In our article on Python in-built functions, one function we saw was iter(). Let us generate an iterator in Python, which we traversed using the next() function.<\/p>\n<p>S, let&#8217;s start Python Iterator Tutorial.<\/p>\n<h3>What are Python3 Iterators?<\/h3>\n<p>An iterator in the Python programming language is an object that you can iterate upon. That is, it returns one object at a time.<\/p>\n<p>Python Iterator, implicitly implemented in constructs like for-loops, comprehensions, and Python generators.<\/p>\n<p>The iter() and next() functions collectively form the iterator protocol.<\/p>\n<p>If we can get an iterable from an object in Python, it is iterable. Examples include Python lists, Python tuples, and Python strings.<\/p>\n<h3>How to Create a Python Iterator?<\/h3>\n<p>To build a Python 3 iterator, we use the iter() and next() functions. Let\u2019s begin with iter() to create an iterator.<\/p>\n<p>First, we create a list of events to store even numbers from 2 to 10.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; evens=[2,4,6,8,10]<\/pre>\n<p>Then, we apply the iter() function to this Python list to create a Python iterator object. We store it in the variable evenIterator.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; evenIterator=iter(evens)\r\n&gt;&gt;&gt; evenIterator<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;list_iterator object at 0x05E35410&gt;<\/div>\n<p>Remember, it does not have to be a list you create an iterator on.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; iter((1,3,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;tuple_iterator object at 0x05E35550&gt;<\/div>\n<p>Now, to access the first element, we apply the function next() on the Python iterator object.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>We do the same for the next element(s) as well.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<p>We have reached the end of the list. When we call it once more, we raise a StopIteration error (exception). The interpreter internally catches it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(evenIterator)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#442&gt;&#8221;, line 1, in &lt;module&gt;next(evenIterator)StopIteration<\/p>\n<\/div>\n<p><strong>__next__()<\/strong><\/p>\n<p>You can also traverse the Python iterator using the __next__() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; nums=[1,2,3]\r\n&gt;&gt;&gt; numIter=iter(nums)\r\n&gt;&gt;&gt; numIter.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(numIter)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; numIter.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; numIter.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#448&gt;&#8221;, line 1, in &lt;module&gt;numIter.__next__()StopIteration<\/div>\n<p>We can see this with the dir() function we saw in the built-in functions in Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; dir(numIter)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;__class__&#8217;, &#8216;__delattr__&#8217;, &#8216;__dir__&#8217;, &#8216;__doc__&#8217;, &#8216;__eq__&#8217;, &#8216;__format__&#8217;, &#8216;__ge__&#8217;, &#8216;__getattribute__&#8217;, &#8216;__gt__&#8217;, &#8216;__hash__&#8217;, &#8216;__init__&#8217;, &#8216;__init_subclass__&#8217;, &#8216;__iter__&#8217;, &#8216;__le__&#8217;, &#8216;__length_hint__&#8217;, &#8216;__lt__&#8217;, &#8216;__ne__&#8217;, &#8216;__new__&#8217;, &#8216;__next__&#8217;, &#8216;__reduce__&#8217;, &#8216;__reduce_ex__&#8217;, &#8216;__repr__&#8217;, &#8216;__setattr__&#8217;, &#8216;__setstate__&#8217;, &#8216;__sizeof__&#8217;, &#8216;__str__&#8217;, &#8216;__subclasshook__&#8217;]<\/div>\n<h3>For-loop in Python Iterator<\/h3>\n<p>You can also use a for loop in Python to iterate over an iterable like a Python list or a Python tuple.<\/p>\n<ul>\n<li>While using a loop, avoid managing the index manually.<\/li>\n<li>A loop lets you go through iterable objects like a dictionary, a list, or a custom iterator.<\/li>\n<li>In a loop, you can apply the same operation to every item .<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in 'Python':\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">P<br \/>\ny<br \/>\nt<br \/>\nh<br \/>\no<br \/>\nn<\/div>\n<p>But how is this actually implemented? Let\u2019s take a look.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; iter_obj=iter('Python')\r\n&gt;&gt;&gt; while True:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i=next(iter_obj)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(i)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 except StopIteration:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">P<br \/>\ny<br \/>\nt<br \/>\nh<br \/>\no<br \/>\nn<\/div>\n<p>So, this is how the above for loop is actually implemented.<\/p>\n<h3>How to Create Your Own Python Iterator?<\/h3>\n<p>Now you know how to use an iterator with the iter() and next() functions. But we won\u2019t stop here. We will now start from scratch.<\/p>\n<p>We implement the following class to create an iterator in Python for the squares of numbers from 1 to max.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class PowTwo:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def __init__(self,max=0):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.max=max\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def __iter__(self):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.n=0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return self\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def __next__(self):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if self.n&lt;=self.max:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result=2**self.n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.n+=1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return result\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 raise StopIteration<\/pre>\n<p>Here, __init__() is to take the value of max. Then, we create a Python object \u2018a\u2019 of the class, with the argument 4.<\/p>\n<p>Then, we create an iterator in Python using iter(). Next, we use the next() function to get the elements one by one.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=PowTwo(4)\r\n&gt;&gt;&gt; i=iter(a)\r\n&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">16<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#484&gt;&#8221;, line 1, in &lt;module&gt;next(i)File &#8220;&lt;pyshell#476&gt;&#8221;, line 13, in __next__raise StopIterationStopIteration<\/div>\n<p>Alternatively, you can use the __iter__() and __next__() methods for this object.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j=a.__iter__()\r\n&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">16<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; j.__next__()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#491&gt;&#8221;, line 1, in &lt;module&gt;j.__next__()File &#8220;&lt;pyshell#476&gt;&#8221;, line 13, in __next__raise StopIterationStopIteration<\/div>\n<p>Internally, the iter() function calls the __iter__() method.<\/p>\n<h3>Infinite Python3 Iterator<\/h3>\n<p>It is indeed possible to create an iterator in Python that never exhausts. The iter() function can take another argument, called the \u2018sentinel\u2019, which keeps a watch.<\/p>\n<p>As long as the first value isn\u2019t the same as the sentinel, the Python iterator does not exhaust.<\/p>\n<p>We know that the int() function, without a parameter, returns 0.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; int()<\/pre>\n<p>Now, we call iter() on two arguments- int and 1.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=iter(int,1)<\/pre>\n<p>This Python iterator will never exhaust; it is infinite. This is because 0 is never equal to 1.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(a)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(a)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(a)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(a)<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(a)<\/pre>\n<p>And so on.<\/p>\n<p>To create an infinite Python iterator using a class, take the following example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class Even:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def __iter__(self):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.num=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return self\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 def __next__(self):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 num=self.num\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.num+=2\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return num\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\r\n&gt;&gt;&gt; e=Even()\r\n&gt;&gt;&gt; i=iter(e)\r\n&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<p>This Python iterates on even numbers beginning at 2, ending nowhere. So, you must be careful to include a terminating condition.<\/p>\n<h3>Benefits of Python Iterator<\/h3>\n<p>An iterator in Python saves resources. To get all the elements, only one element is stored in the memory at a time. Unlike this, a list would have to store all the values at once.<\/p>\n<h3>Python Interview Questions on Iterators<\/h3>\n<p>1. What are Python Iterators?<\/p>\n<p>2. Why is an iterator used in Python?<\/p>\n<p>3. How do you write an Iterator in Python?<\/p>\n<p>4. What are iterators and iterables in Python?<\/p>\n<p>5. Why is iteration important in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>An iterator is the worker returned by iter(obj) that actually produces items one by one. It must keep its own internal state and provide a __next__() method that raises StopIteration when done. Every call to next(it) yields the next value, letting a loop progress without copying the whole dataset.<\/p>\n<p>However, we can make our own iterator in Python with a Python class. Finally, we looked at infinite iterators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Iterator Tutorial, we will learn what a Python iterator. We will also discuss how to create our own __iter__() and __next__() methods, building a Python iterator, a for loop in Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":6733,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[1774,1784,2200,2202,2215,4853,6698,15867],"class_list":["post-6731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-benefits-of-iterator-in-python","tag-benefits-of-python-iterator","tag-building-a-python-iterator","tag-building-an-iterator-in-python","tag-building-python-iterator","tag-for-loop-in-python","tag-infinite-python-iterator","tag-what-is-python-iterators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Python Iterator - Create your own Iterator - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about iterator in python with syntax, examples and benefits, for loop in python iterator, Infinite Python Iterators.\" \/>\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-iterator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Python Iterator - Create your own Iterator - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about iterator in python with syntax, examples and benefits, for loop in python iterator, Infinite Python Iterators.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-iterator\/\" \/>\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-01-29T07:24:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:10:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Python Iterator - Create your own Iterator - DataFlair","description":"Learn about iterator in python with syntax, examples and benefits, for loop in python iterator, Infinite Python Iterators.","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-iterator\/","og_locale":"en_US","og_type":"article","og_title":"What is Python Iterator - Create your own Iterator - DataFlair","og_description":"Learn about iterator in python with syntax, examples and benefits, for loop in python iterator, Infinite Python Iterators.","og_url":"https:\/\/data-flair.training\/blogs\/python-iterator\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-29T07:24:30+00:00","article_modified_time":"2026-04-28T09:10:29+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"What is Python Iterator &#8211; Create your own Iterator","datePublished":"2018-01-29T07:24:30+00:00","dateModified":"2026-04-28T09:10:29+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/"},"wordCount":965,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.jpg","keywords":["Benefits of Iterator in Python","Benefits of Python Iterator","Building a python Iterator","Building an Iterator in Python","Building Python Iterator","for loop in python","Infinite Python Iterator","what is Python Iterators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-iterator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/","url":"https:\/\/data-flair.training\/blogs\/python-iterator\/","name":"What is Python Iterator - Create your own Iterator - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.jpg","datePublished":"2018-01-29T07:24:30+00:00","dateModified":"2026-04-28T09:10:29+00:00","description":"Learn about iterator in python with syntax, examples and benefits, for loop in python iterator, Infinite Python Iterators.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-iterator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Iterators-in-Python.jpg","width":1200,"height":628,"caption":"Introduction on Python Iterator"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-iterator\/#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":"What is Python Iterator &#8211; Create your own Iterator"}]},{"@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\/6731","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=6731"}],"version-history":[{"count":18,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6731\/revisions"}],"predecessor-version":[{"id":148008,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6731\/revisions\/148008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/6733"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}