{"id":1182,"date":"2020-11-17T01:36:42","date_gmt":"2020-11-17T01:36:42","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1182"},"modified":"2025-03-27T15:00:12","modified_gmt":"2025-03-27T15:00:12","slug":"python-iterator-vs-iterable","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterator-vs-iterable\/","title":{"rendered":"Python Iterator vs Iterable"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python iterator and iterable and their differences.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='iterators'>Iterators <a href=\"#iterators\" class=\"anchor\" id=\"iterators\" title=\"Anchor for Iterators\">#<\/a><\/h2>\n\n\n\n<p>An <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterators\/\">iterator<\/a> is an object that implements the iterator protocol. In other words, an iterator is an object that implements the following methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__iter__<\/code> returns the iterator object itself.<\/li>\n\n\n\n<li><code>__next__<\/code> returns the next element.<\/li>\n<\/ul>\n\n\n\n<p>Once you complete iterating a collection using an iterator, the iterator becomes exhausted. It means that you cannot use the iterator object anymore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='iterables'>Iterables <a href=\"#iterables\" class=\"anchor\" id=\"iterables\" title=\"Anchor for Iterables\">#<\/a><\/h2>\n\n\n\n<p>An <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterable<\/a> is an object that you can iterate over.<\/p>\n\n\n\n<p>An object is iterable when it implements the <code>__iter__<\/code> method. And its <code>__iter__<\/code> method returns a new iterator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='examining-the-built-in-list-and-list-iterator'>Examining the built-in list and list iterator <a href=\"#examining-the-built-in-list-and-list-iterator\" class=\"anchor\" id=\"examining-the-built-in-list-and-list-iterator\" title=\"Anchor for Examining the built-in list and list iterator\">#<\/a><\/h2>\n\n\n\n<p>In Python, a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> is an ordered collection of items. It&#8217;s also an iterable because a list object has the <code>__iter__<\/code> method that returns an iterator. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">numbers = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n\nnumber_iterator = numbers.__iter__()\nprint(type(number_iterator))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=bnVtYmVycyA9IFsxLCAyLCAzXQoKbnVtYmVyX2l0ZXJhdG9yID0gbnVtYmVycy5fX2l0ZXJfXygpCnByaW50KHR5cGUobnVtYmVyX2l0ZXJhdG9yKSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">list_iterator<\/span>'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>__iter__<\/code> method returns an iterator with the type <code>list_iterator<\/code>.<\/p>\n\n\n\n<p>Because the <code>list_iterator<\/code> implements the <code>__iter__<\/code> method, you can use the <code>iter<\/code> built-in function to get the iterator object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">numbers = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\nnumber_iterator = iter(numbers)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>list_iterator<\/code> also implements the <code>__next__<\/code> method, you can use the built-in function <code>next<\/code> to iterate over the list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">numbers = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n\nnumber_iterator = iter(numbers)\n\nnext(number_iterator)\nnext(number_iterator)\nnext(number_iterator)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you call the <code>next<\/code> function once more, you&#8217;ll get a <code>StopIteration<\/code> exception.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">next(number_iterator)<\/code><\/span><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">StopIteration<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is because the list iterator has been exhausted. To iterate the list again, you need to create a new iterator.<\/p>\n\n\n\n<p>This illustrates the separating the list from its iterator. The list is created once while the iterator is created every time you need to iterate over the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-iterator-and-iterable'>Python Iterator and Iterable <a href=\"#python-iterator-and-iterable\" class=\"anchor\" id=\"python-iterator-and-iterable\" title=\"Anchor for Python Iterator and Iterable\">#<\/a><\/h2>\n\n\n\n<p>The following defines the <code>Colors<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Colors<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.rgb = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>]\n        self.__index = <span class=\"hljs-number\">0<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__iter__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__next__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self.__index &amp;gt;= len(self.rgb):\n            <span class=\"hljs-keyword\">raise<\/span> StopIteration\n\n        <span class=\"hljs-comment\"># return the next color<\/span>\n        color = self.rgb&#91;self.__index]\n        self.__index += <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">return<\/span> color<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>Colors<\/code> class plays two roles: iterable and iterator.<\/p>\n\n\n\n<p>The <code>Colors<\/code> class is an iterator because it implements both <code>__iter__<\/code> and <code>__next__<\/code> method. The <code>__iter__<\/code> method returns the object itself. And the <code>__next__<\/code> method returns the next item from a list.<\/p>\n\n\n\n<p>The <code>Colors<\/code> class is also an iterable because it implements the <code>__iter__<\/code> method that returns an object itself, which is an iterator.<\/p>\n\n\n\n<p>The following creates a new instance of the <code>Colors<\/code> class and iterates over its elements using a <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-loop-list\/\">for<\/a><\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = Colors()\n\n<span class=\"hljs-keyword\">for<\/span> color <span class=\"hljs-keyword\">in<\/span> colors:\n    print(color)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once you complete iterating, the <code>colors<\/code> object becomes useless. If you attempt to iterate it again, you&#8217;ll get a <code>StopIteration<\/code> exception:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">next(colors)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">StopIteration<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you use the <code>for<\/code> loop, you&#8217;ll get nothing back. The iterator is empty:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> color <span class=\"hljs-keyword\">in<\/span> colors:\n    print(color)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To iterate again, you need to create a new <code>colors<\/code> object with the <code>rgb<\/code> attribute. This is inefficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='separating-an-iterator-from-an-iterable'>Separating an iterator from an iterable <a href=\"#separating-an-iterator-from-an-iterable\" class=\"anchor\" id=\"separating-an-iterator-from-an-iterable\" title=\"Anchor for Separating an iterator from an iterable\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s separate the color iterator from its iterable like what Python does with the list iterator and list.<\/p>\n\n\n\n<p>The following defines the <code>Colors<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Colors<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.rgb = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>]\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> len(self.rgb)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following defines the <code>ColorIterator<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ColorIterator<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, colors)<\/span>:<\/span>\n        self.__colors = colors\n        self.__index = <span class=\"hljs-number\">0<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__iter__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__next__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self.__index &gt;= len(self.__colors):\n            <span class=\"hljs-keyword\">raise<\/span> StopIteration\n\n        <span class=\"hljs-comment\"># return the next color<\/span>\n        color = self.__colors.rgb&#91;self.__index]\n        self.__index += <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">return<\/span> color<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>__init__<\/code> method accepts an iterable which is an instance of the <code>Colors<\/code> class.<\/li>\n\n\n\n<li>The <code>__iter__<\/code> method returns the iterator itself.<\/li>\n\n\n\n<li>The __next__ method returns the next element from the <code>Colors<\/code> object.<\/li>\n<\/ul>\n\n\n\n<p>The following shows how to use the <code>ColorIterator<\/code> to iterate over the <code>Colors<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = Colors()\ncolor_iterator = ColorIterator(colors)\n\n<span class=\"hljs-keyword\">for<\/span> color <span class=\"hljs-keyword\">in<\/span> color_iterator:\n    print(color)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To iterate the <code>Colors<\/code> object again, you just need to create a new instance of the <code>ColorIterator<\/code>.<\/p>\n\n\n\n<p>There&#8217;s one problem!<\/p>\n\n\n\n<p>When you want to iterate the <code>Colors<\/code> object, you need to manually create a new <code>ColorIterator<\/code> object. And you also need to remember the iterator name <code>ColorIterator<\/code>.<\/p>\n\n\n\n<p>It would be great if you can automate this. To do it, you can make the <code>Colors<\/code> class iterable by implementing the <code>__iter__<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Colors<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.rgb = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>]\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> len(self.rgb)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__iter__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> ColorIterator(self)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>__iter__<\/code> method returns a new instance of the <code>ColorIterator<\/code> class.<\/p>\n\n\n\n<p>Now, you can iterate the <code>Colors<\/code> object without explicitly creating the <code>ColorIterator<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = Colors()\n\n<span class=\"hljs-keyword\">for<\/span> color <span class=\"hljs-keyword\">in<\/span> colors:\n    print(color)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Internally, the <code>for<\/code> loop calls the <code>__iter__<\/code> method of the <code>colors<\/code> object to get the iterator and uses this iterator to iterate over the elements of the <code>colors<\/code> object.<\/p>\n\n\n\n<p>The following places the <code>ColorIterator<\/code> class inside the <code>Colors<\/code> class to encapsulate them into a single class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Colors<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.rgb = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>]\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> len(self.rgb)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__iter__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.ColorIterator(self)\n\n    <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ColorIterator<\/span>:<\/span>\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, colors)<\/span>:<\/span>\n            self.__colors = colors\n            self.__index = <span class=\"hljs-number\">0<\/span>\n\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__iter__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n            <span class=\"hljs-keyword\">return<\/span> self\n\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__next__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n            <span class=\"hljs-keyword\">if<\/span> self.__index &gt;= len(self.__colors):\n                <span class=\"hljs-keyword\">raise<\/span> StopIteration\n\n            <span class=\"hljs-comment\"># return the next color<\/span>\n            color = self.__colors.rgb&#91;self.__index]\n            self.__index += <span class=\"hljs-number\">1<\/span>\n            <span class=\"hljs-keyword\">return<\/span> color\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An iterable is an object that implements the <code>__iter__<\/code> method which returns an iterator.<\/li>\n\n\n\n<li>An iterator is an object that implements the <code>__iter__<\/code> method which returns itself and the <code>__next__<\/code> method which returns the next element.<\/li>\n\n\n\n<li>Iterators are also iterables. <\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1182\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterator-vs-iterable\/\"\n\t\t\t\tdata-post-title=\"Python Iterator vs Iterable\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1182\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterator-vs-iterable\/\"\n\t\t\t\tdata-post-title=\"Python Iterator vs Iterable\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about Python iterator and iterable and their differences.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1182","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1182"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1182\/revisions"}],"predecessor-version":[{"id":7130,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1182\/revisions\/7130"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}