

{"id":6764,"date":"2018-01-30T09:42:17","date_gmt":"2018-01-30T09:42:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6764"},"modified":"2026-04-28T14:41:24","modified_gmt":"2026-04-28T09:11:24","slug":"python-generator-vs-iterator","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/","title":{"rendered":"Python Generators vs Iterators"},"content":{"rendered":"<p>It\u2019s been more than a month since we began our journey with <a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\"><strong>Python Programming Language<\/strong><\/a>. Through the days, we have also learned concepts like Python generators and iterators in Python.<\/p>\n<p>Python iterators and generators are essential tools used for processing, allowing developers to go through the big datasets one by one. While iterators provide you with a guide on how to approach, generators offer a simplified and automated method to achieve the result using specialized functions.<\/p>\n<p>We know their functionalities. But how are they different? In this lesson, we discuss the comparison of Python generator vs iterator. Before we proceed, let&#8217;s discuss <a href=\"https:\/\/data-flair.training\/blogs\/python-syntax-semantics\/\"><strong>Python Syntax<\/strong><\/a>.<\/p>\n<h3>Introduction to Python Generators<\/h3>\n<p>A generator function in python lends us a sequence of values to python iterate on. The following is an example of generators in <a href=\"https:\/\/data-flair.training\/blogs\/python-features\/\"><strong>python<\/strong><\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def even(x):\r\n       while(x!=0):\r\n           if x%2==0:\r\n                 yield x\r\n              x-=1\r\n&gt;&gt;&gt; for i in even(8):\r\n               print(i)<\/pre>\n<p>8<br \/>\n6<br \/>\n4<br \/>\n2<br \/>\nTo see the generator in detail, refer to our\u00a0article on <a href=\"https:\/\/data-flair.training\/blogs\/python-generators\/\"><strong>Python Generator<\/strong><\/a>.<\/p>\n<h3>Introduction to Python Iterators<\/h3>\n<p>A Python iterator returns us an iterator object, one value at a time. Let\u2019s take an example of an iterator in Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; iter_obj=iter([3,4,5])\r\n&gt;&gt;&gt; next(iter_obj)<\/pre>\n<p>3<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(iter_obj)<\/pre>\n<p>4<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(iter_obj)<\/pre>\n<p>5<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; next(iter_obj)<\/pre>\n<p>Traceback (most recent call last):<\/p>\n<p>File &#8220;&lt;pyshell#560&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>next(iter_obj)<\/p>\n<p>StopIteration<\/p>\n<p>For more insight, check our\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/python-iterator\/\"><strong>Python Iterator tutorial<\/strong><\/a>.<\/p>\n<h3>Comparison Between Generator vs Iterator in Python<\/h3>\n<p>Let&#8217;s see the difference between Iterators and Generators in Python.<\/p>\n<p>1. In creating a Python generator, we use a function. But in creating an iterator in Python, we use the iter() and next() functions.<\/p>\n<p>2. A generator in Python makes use of the \u2018yield\u2019 keyword. A Python iterator doesn\u2019t.<\/p>\n<p>3. Python generator saves the states of the local variables every time \u2018yield\u2019 pauses the <a href=\"https:\/\/data-flair.training\/blogs\/loops-in-python\/\"><strong>loop in python<\/strong><\/a>. An iterator does not make use of local variables; all it needs is iterable to iterate on.<\/p>\n<p>4. A generator may have any number of \u2018yield\u2019 statements.<\/p>\n<p>5. You can implement your own iterator using a <a href=\"https:\/\/data-flair.training\/blogs\/python-classes\/\"><strong>python class<\/strong><\/a>; a generator does not need a class in Python.<\/p>\n<p>6. To write a Python generator, you can either use a <a href=\"https:\/\/data-flair.training\/blogs\/python-functions\/\"><strong>Python function<\/strong><\/a> or a comprehension. But for an iterator, you must use the iter() and next() functions.<\/p>\n<p>7. Generators in Python let us write fast and compact code. This is an advantage over Python iterators. They are also simpler to code than a custom iterator.<\/p>\n<p>8. Python iterators are more memory-efficient. Let&#8217;s see this with an example below:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def func():\r\n       i=1\r\n       while i&gt;0:\r\n                 yield i\r\n                 i-=1\r\n&gt;&gt;&gt; for i in func():\r\n             print(i)<\/pre>\n<p>1<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; func().__sizeof__()<\/pre>\n<p>32<br \/>\nHere, we got 32. But for a Python iterator, we get 16.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; iter([1,2]).__sizeof__()<\/pre>\n<p>16<\/p>\n<p>9. A generator returns a generator. Below is an example to understand it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; f=func()\r\n&gt;&gt;&gt; type(f)<\/pre>\n<p>&lt;class &#8216;generator&#8217;&gt;<br \/>\nHowever, an iterator returns an iterator object.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; i=iter({1,3,2})\r\n&gt;&gt;&gt; type(i)<\/pre>\n<p>&lt;class &#8216;set_iterator&#8217;&gt;<\/p>\n<p>10. python Generator provides even more functionality as co-routines.<\/p>\n<h3>Relationship Between Python Generators and Iterators<\/h3>\n<h4><strong>a. A python generator is an iterator<\/strong><\/h4>\n<p>A generator in python is a subclass of Iterator. To prove this, we use the issubclass() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import collections,types\r\n&gt;&gt;&gt; issubclass(types.GeneratorType,collections.Iterator)<\/pre>\n<p>True<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; issubclass(collections.Generator,collections.Iterator)<\/pre>\n<p>True<\/p>\n<pre class=\"EnlighterJSRAW\">\u00a0&gt;&gt;&gt;\u00a0issubclass(collections.Iterator,types.GeneratorType)<\/pre>\n<p>False<br \/>\n<strong>b. Python iterator is an iterable<\/strong><\/p>\n<p>An iterator in python is a subclass of Iterable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; issubclass(collections.Iterator,collections.Iterable)<\/pre>\n<p>True<\/p>\n<h3>Conclusion<\/h3>\n<p>Every generator is an iterator, but not every iterator is a generator. An iterator may be a class with __next__ that computes items, while a generator is created by a function with yield, letting Python build the iterator for you.<\/p>\n<p>Hence, we study the difference between Python generator vs iterator and we can say every generator is an iterator in <a href=\"https:\/\/data-flair.training\/blogs\/reasons-why-should-i-learn-python\/\"><strong>Python<\/strong><\/a>, not every python iterator is a generator. Both come in handy and have their own perks. Tell us what you think in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s been more than a month since we began our journey with Python Programming Language. Through the days, we have also learned concepts like Python generators and iterators in Python. Python iterators and generators&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":6771,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2755,3882,10555,10624,15865,15866],"class_list":["post-6764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-comparison-between-python-iterator-and-generator","tag-difference-between-python-generator-and-iterator","tag-python-generator-vs-iterator","tag-python-iterator-vs-generator","tag-what-is-python-generator","tag-what-is-python-iterator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Generators vs Iterators - DataFlair<\/title>\n<meta name=\"description\" content=\"Python generator vs Iterator-what is python Generator &amp; Iterator,relationship between Python Iterator &amp; Generator,difference between python Iterator vs Generator\" \/>\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-generator-vs-iterator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Generators vs Iterators - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python generator vs Iterator-what is python Generator &amp; Iterator,relationship between Python Iterator &amp; Generator,difference between python Iterator vs Generator\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-generator-vs-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-30T09:42:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:11:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"5000\" \/>\n\t<meta property=\"og:image:height\" content=\"2617\" \/>\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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Generators vs Iterators - DataFlair","description":"Python generator vs Iterator-what is python Generator & Iterator,relationship between Python Iterator & Generator,difference between python Iterator vs Generator","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-generator-vs-iterator\/","og_locale":"en_US","og_type":"article","og_title":"Python Generators vs Iterators - DataFlair","og_description":"Python generator vs Iterator-what is python Generator & Iterator,relationship between Python Iterator & Generator,difference between python Iterator vs Generator","og_url":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-30T09:42:17+00:00","article_modified_time":"2026-04-28T09:11:24+00:00","og_image":[{"width":5000,"height":2617,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Generators vs Iterators","datePublished":"2018-01-30T09:42:17+00:00","dateModified":"2026-04-28T09:11:24+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/"},"wordCount":570,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.jpg","keywords":["Comparison Between Python Iterator and Generator","Difference Between Python Generator and Iterator","python generator vs iterator","python iterator vs generator","what is Python Generator","what is python Iterator"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/","url":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/","name":"Python Generators vs Iterators - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.jpg","datePublished":"2018-01-30T09:42:17+00:00","dateModified":"2026-04-28T09:11:24+00:00","description":"Python generator vs Iterator-what is python Generator & Iterator,relationship between Python Iterator & Generator,difference between python Iterator vs Generator","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-iterator\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Generators-vs-Iterators-01.jpg","width":5000,"height":2617,"caption":"Difference Between Python Generator vs Iterator"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-generator-vs-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":"Python Generators vs Iterators"}]},{"@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\/6764","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=6764"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6764\/revisions"}],"predecessor-version":[{"id":148012,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6764\/revisions\/148012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/6771"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}