{"id":19834,"date":"2022-09-18T19:11:00","date_gmt":"2022-09-18T13:41:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/"},"modified":"2024-10-15T01:02:17","modified_gmt":"2024-10-14T19:32:17","slug":"iterator-in-python","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/","title":{"rendered":"Iterator in Python | A Step-by-Step Tutorial"},"content":{"rendered":"\n<p>In Python, an iterator is an object that can be iterated upon. This means that you can traverse through all the values in the object. Iterators are used in both for loops and while loops. Learn more with the help of this blog post. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#what-is-an-iterator-in-python\">What is iterator in Python?<\/a><\/li>\n\n\n\n<li><a href=\"#iterator-and-iterable\">Iterator and Iterable&nbsp; <\/a><\/li>\n\n\n\n<li><a href=\"#which-methods-are-defines-in-an-iterator-class\">Which methods are defined in an iterator class in python?<\/a>\n<ol class=\"wp-block-list\">\n<li><a href=\"#iter\">__iter__()<\/a>&nbsp;<\/li>\n\n\n\n<li><a href=\"#next\">__next__()<\/a><\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><a href=\"#iterating-through-an-iterator\">Iterating Through an Iterator<\/a><\/li>\n\n\n\n<li><a href=\"#create-an-iterator\">Create an Iterator&nbsp;<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-iterator-in-python\"><strong>What is an iterator in Python?<\/strong><\/h2>\n\n\n\n<p>An iterator in <a aria-label=\"Python (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/blog\/python-tutorial-for-beginners-a-complete-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> is an object that contains a countable number of elements that can be iterated upon. In simpler words, we can say that Iterators are objects that allow you to traverse through all the elements of a collection and return one element at a time. More specifically, we say that an iterator is an object that implements the iterator protocol. We are going to discuss this protocol in the later section.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"iterator-and-iterable\"><strong>Iterator and Iterable<\/strong><\/h2>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/Iterators-in-Python.jpg\"><img decoding=\"async\" width=\"588\" height=\"285\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/Iterators-in-Python.jpg\" alt=\"iterator in Python\" class=\"wp-image-19837\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/Iterators-in-Python.jpg 588w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/Iterators-in-Python-300x145.jpg 300w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><\/figure>\n\n\n\n<p>Iterable is an object, which one can iterate over. It generates an Iterator when passed to the iter() method. Lists, tuples, dictionaries, strings and sets are all iterable objects. They are iterable containers that you can convert into an iterator.<\/p>\n\n\n\n<p>Note that every iterator is also an iterable, but not every iterable is an iterator. For example, a tuple is iterable, but it is not an iterator. An iterator can be created from an iterable by using the function iter(). Thus, when we pass this tuple to an iter() function, we will get an iterator. Actually, this is possible because the class of an object has a method __iter__, which returns an iterator.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"which-methods-are-defined-in-an-iterator-class-in-python\"><strong>Which methods are defined in an iterator class in python?<\/strong><\/h2>\n\n\n\n<p>As mentioned above, an object that implements the iterator protocol is an iterator. But what is this iterator protocol? An iterator protocol is nothing but a specific class in Python which further has the __iter__()&nbsp; and __next__()&nbsp; methods.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"__iter__\"><strong>__iter__()<\/strong><\/h3>\n\n\n\n<p>This method returns the iterator object itself as mentioned above. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python\/C API.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"__next__\"><strong>__next__()<\/strong><\/h3>\n\n\n\n<p>This method returns the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python\/C API.<br><\/p>\n\n\n\n<p>Here is a simple example, describing these methods:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># define a iterable such as a list\n&gt;&gt;&gt; list1 = &#091;0, 1, 2]\n# get an iterator using iter()\n&gt;&gt;&gt; iter1 = list1.__iter__()\n#iertae the item using __next__method\n&gt;&gt;&gt; print(iter1.__next__())\n0<\/code><\/pre>\n\n\n\n<p>We can use the __next__ method again to get the next item, in fact, we can use it as many times as we want and each time it'll return the next item of the iterator. But when there are no elements in the iterator to return, it throws an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"iterating-through-an-iterator\"><strong>Iterating Through an Iterator<\/strong><\/h2>\n\n\n\n<p>Now that we have an iterator object, what are the various ways by which we can traverse its elements such that we only get one item at a time? In this section, we are going to see a few ways in which we can do so, also we are going to show some of the examples for you to understand iterators better:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-the-next-function\"><strong>Using the next() function<\/strong><\/h4>\n\n\n\n<p>We can directly use next() function or __next__() method to traverse the elements as shown in the examples below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># define a iterable such as a list\n&gt;&gt;&gt; list1 = &#091;1, 2, 0]\n# get an iterator using iter()\n&gt;&gt;&gt; iter1 = iter(list1)\n# iterate through it using next()\n&gt;&gt;&gt; print(next(iter1))\n1\n# next(obj) is same as obj.__next__()\n&gt;&gt;&gt; print(iter1.__next__())\n2\n&gt;&gt;&gt; print(next(iter1))\n0\n# This will raise error, no items left\n&gt;&gt;&gt; next(my_iter)\nTraceback (most recent call last):\n  File \"&lt;pyshell#8&gt;\", line 1, in &lt;module&gt;\n    next(my_iter)<\/code><\/pre>\n\n\n\n<p>Now let us see how we can traverse a string iterator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># define a iterable such as a string\n&gt;&gt;&gt; string = \"hi\"\n# get an iterator using iter()\n&gt;&gt;&gt; iter1 = iter(string)\n# iterate through it using next()\n&gt;&gt;&gt; print(next(iter1))\nh\n&gt;&gt;&gt; print(next(iter1))\ni\n# This will raise error, no items left\n&gt;&gt;&gt; next(my_iter)\nTraceback (most recent call last):\n  File \"&lt;pyshell#8&gt;\", line 1, in &lt;module&gt;\n    next(my_iter)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterating-an-iterator-using-while-loop-and-for-loop\"><strong>Iterating an iterator using while loop and for loop<\/strong><\/h4>\n\n\n\n<p>Since we are using the next() function repeatedly, why not use a while loop and make our work a bit easier. Also, we will use exception handling to take care of the error that we get when the iterator has no elements left. So whenever the error \u201cStopIteration\u201d occurs, the control goes to the except block and the break statement is executed. Here is an example:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># define an iterable such as a list\nlist1=&#091;1,2,3,4,5,6,7,8,9,0]\n\n# get an iterator using iter()\niter1=iter(list1)\n# infinite loop\nwhile True:\n    try:\n        # get the next item\n        print(next(iter1))\n        # do something with element\n    except StopIteration:\n        # if StopIteration is raised, break from loop\n        break<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>1 <br>2 <br>3 <br>4 <br>5 <br>6 <br>7 <br>8 <br>9 <br>0<\/p>\n\n\n\n<p>There is a much easier way to do this also by using a For loop. The for loop does all this under the hood, thus, you don\u2019t need to explicitly call the iter() and next() functions. Here are a few examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1=&#091;1,2,3,4,5,6,7,8,9,0]\nfor i in list1:\n    print(i)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>1 <br>2 <br>3 <br>4 <br>5 <br>6 <br>7 <br>8 <br>9 <br>0<\/p>\n\n\n\n<p>Now let us take some more example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list2=&#091;1,2,\"hello\",&#091;9,8,7],(11,12),{'one': 'husaain'}]\nfor i in list2:\n    print(i)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>1 <br>2 <br>hello <br>[9, 8, 7] <br>(11, 12) <br>{'one': 'husaain'}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string=\"Hello World \"\nfor i in string:\n    print(i)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>H <br>e <br>l <br>l <br>o &nbsp; <br><br>W <br>o <br>r <br>l <br>d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-an-iterator\"><strong>Create an Iterator<\/strong><\/h2>\n\n\n\n<p>Till now we have only used the inbuilt iterables such as lists or strings, but we can also build an iterator from scratch is easy in Python. We just have to implement the __iter__() and the __next__() methods.<\/p>\n\n\n\n<p>Here is our own custom Iterator that returns an even number or 1 every time we iterate upon it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Evenit:\n \n    def __init__(self, max=0):\n        self.max = max\n\n    def __iter__(self):\n        self.n = 0\n        return self\n\n    def __next__(self):\n        if self.n &lt;= self.max:\n            if self.n % 2 ==0:\n                result=self.n\n                self.n += 1\n                return result\n            else:\n                self.n += 1\n                return 1\n        else:\n            raise StopIteration\n\n\n# create an object\nnumbers = Evenit(10)\n\nfor i in numbers:\n\tprint(i)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>0 <br>1 <br>2 <br>1 <br>4 <br>1 <br>6 <br>1 <br>8 <br>1 <br>10<\/p>\n\n\n\n<p>As we can see, this iterator returns even numbers up to 10 (because we have given the argument to the event as 10), and whenever it encounters an odd number, it just returns 1. In a similar manner, you can create your own iterator.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>Creating custom iterators and mastering loops is a major milestone in your programming journey. However, if any of the concepts in this guide still feel a bit complex, remember that every expert started with the basics. To ensure you have a rock-solid foundation, consider exploring the modules in our Free Python Course. It is tailored for beginners to help them learn at their own pace, making it the perfect stepping stone before you dive back into advanced data structures and professional-grade coding.<\/p>\n\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Free Course<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-title-link\">Python Fundamentals for Beginners Free Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master Python basics, from variables to data structures and control flow. Solve real-time problems and build practical skills using Jupyter Notebook.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>13.5 hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>4.55<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-button\">\n                Enroll for Free\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Python, an iterator is an object that can be iterated upon. This means that you can traverse through all the values in the object. Iterators are used in both for loops and while loops. Learn more with the help of this blog post. What is an iterator in Python? An iterator in Python is [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":19838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[],"class_list":["post-19834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>A Step by Step Guide on Iterator in Python<\/title>\n<meta name=\"description\" content=\"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection &amp; return one element at a time.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iterator in Python | A Step-by-Step Tutorial\" \/>\n<meta property=\"og:description\" content=\"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection &amp; return one element at a time.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-18T13:41:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-14T19:32:17+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Iterator in Python | A Step-by-Step Tutorial\",\"datePublished\":\"2022-09-18T13:41:00+00:00\",\"dateModified\":\"2024-10-14T19:32:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/\"},\"wordCount\":909,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/shutterstock_1389877574.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/\",\"name\":\"A Step by Step Guide on Iterator in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/shutterstock_1389877574.jpg\",\"datePublished\":\"2022-09-18T13:41:00+00:00\",\"dateModified\":\"2024-10-14T19:32:17+00:00\",\"description\":\"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection & return one element at a time.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/shutterstock_1389877574.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/shutterstock_1389877574.jpg\",\"width\":1000,\"height\":700,\"caption\":\"iterator in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/iterator-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Iterator in Python | A Step-by-Step Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"A Step by Step Guide on Iterator in Python","description":"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection & return one element at a time.","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:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Iterator in Python | A Step-by-Step Tutorial","og_description":"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection & return one element at a time.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2022-09-18T13:41:00+00:00","article_modified_time":"2024-10-14T19:32:17+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Iterator in Python | A Step-by-Step Tutorial","datePublished":"2022-09-18T13:41:00+00:00","dateModified":"2024-10-14T19:32:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/"},"wordCount":909,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/","url":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/","name":"A Step by Step Guide on Iterator in Python","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg","datePublished":"2022-09-18T13:41:00+00:00","dateModified":"2024-10-14T19:32:17+00:00","description":"Iterator in Python: Iterators are objects that allow you to traverse through all the elements of a collection & return one element at a time.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg","width":1000,"height":700,"caption":"iterator in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/iterator-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Iterator in Python | A Step-by-Step Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574-300x210.jpg",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574-768x538.jpg",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/08\/shutterstock_1389877574.jpg",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"In Python, an iterator is an object that can be iterated upon. This means that you can traverse through all the values in the object. Iterators are used in both for loops and while loops. Learn more with the help of this blog post. What is an iterator in Python? An iterator in Python is&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/19834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=19834"}],"version-history":[{"count":12,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/19834\/revisions"}],"predecessor-version":[{"id":110875,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/19834\/revisions\/110875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/19838"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=19834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=19834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=19834"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=19834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}