{"id":19355,"date":"2020-07-11T11:33:59","date_gmt":"2020-07-11T18:33:59","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19355"},"modified":"2023-12-01T03:54:29","modified_gmt":"2023-12-01T11:54:29","slug":"python-iterator","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-iterator\/","title":{"rendered":"Python Iterator: A Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">How to Create a Python Iterator: A Guide<\/h2>\n\n\n\n<p>Iterators are absolutely everywhere in Python; it\u2019s hard to learn even the basics without hearing the word \u201citerator\u201d or \u201citerable\u201d.<br><\/p>\n\n\n\n<p>An iterator is an object that can be iterated upon. While there are some built-in objects upon which you can iterate, you can also define your own.<br><\/p>\n\n\n\n<p>In this guide, we\u2019re going to talk about Python iterators, how they work, and how you can use them in your code. We\u2019ll constructor our own iterator so that you can learn about their core features in action. The code in this guide is compatible for Python 3.x installations.<br><\/p>\n\n\n\n<p>Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Iterator?<\/h2>\n\n\n\n<p>Iterators are objects upon which you can iterate. An iterator will return data from that object, one item at a time.<br><\/p>\n\n\n\n<p>You can check whether an object is iterable by trying to run it through a <a href=\"https:\/\/careerkarma.com\/blog\/python-for-loop\/\">for loop<\/a>; if you can loop through the object, it means that it is iterable. Strings, lists, tuples, and dictionaries are examples of the built-in data types which are iterable objects.<br><\/p>\n\n\n\n<p>An iterator object in Python must have two methods:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__iter__()<\/code>: This allows you to retrieve an iterator.<\/li>\n\n\n\n<li><code>__next__()<\/code>: This allows you to get the next value in an iterator.<\/li>\n<\/ul>\n\n\n\n<p>The <code>__<\/code> underscores are used in iterators because they denote special functions. To define an iterator, you must use these two special functions. Together, they are called the iterator protocol. These methods are usually pronounced as \u201citer and next methods\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Build an Infinite Iterator<\/h2>\n\n\n\n<p>We\u2019ll start with the basics: building an infinite iterator. This is the most simple type of iterator. It\u2019s so simple that it will keep going on forever. We\u2019ll fix this later in the tutorial.<br><\/p>\n\n\n\n<p>Let\u2019s start by defining a class called Cakes, which will be our iterator class:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class Cakes:\n\tdef __init__(self, value):\n\t\tself.value = value<\/pre><\/div>\n\n\n\n<p>We\u2019ve declared an <code>__init__<\/code> method which will be executed as soon as an object of our class is instantiated. The <code>__init__<\/code> method is called a constructor. In this example, our <code>__init__<\/code> method can take in one value: value.<br><\/p>\n\n\n\n<p>Next, we\u2019re going to create our iterator protocol:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def __iter__(self):\n\treturn self\ndef __next__(self):\n\treturn self.value<\/pre><\/div>\n\n\n\n<p>The <code>__iter__<\/code> method is used to retrieve our iterator object. The <code>__next__<\/code> method allows us to retrieve the next value in the iterable object that we specify.&nbsp;<br><\/p>\n\n\n\n<p>Let\u2019s make sure our code works so far. We\u2019ll do that by defining an object of our class like this:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>cake = Cakes([\"Vanilla Sponge\"])<\/pre><\/div>\n\n\n\n<p>So far, so good! You may be thinking to yourself: why isn\u2019t this code doing anything? That\u2019s because our class only returns an iterator function.<br><\/p>\n\n\n\n<p>We\u2019ve got to iterate over our iterator to check if it works. We can do so using a for-in loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>for c in cake:\n\tprint(c)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Vanilla Sponge\nVanilla Sponge\n\u2026<\/pre><\/div>\n\n\n\n<p>The curse of the Vanilla Sponge! It just keeps being printed to the console. What\u2019s going on? We\u2019ve just created our first iterator. It is very simple, but it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Iterators Using for-in Loops in Python<\/h2>\n\n\n\n<p>Before we make our iterator useful, let\u2019s take a moment to revise the concept of a for-in loop. In our last example, we wrote a for-in loop to test our code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>for c in cake:\n\tprint(c)<\/pre><\/div>\n\n\n\n<p>The way that a for-in loop works is that it first creates an iterator of an object using an <code>__iter__()<\/code> method. A for-in loop then calls <code>__next__()<\/code> until no value is returned.<br><\/p>\n\n\n\n<p>You can think of a <code>for-in<\/code> loop as a simple <code>while<\/code> loop. While there are values that an iterator can return, a for-in loop will return those values.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Useful Iterator<\/h2>\n\n\n\n<p>Our last iterator isn\u2019t very useful. It only prints out the same value over and over again. With that said, it is still an iterator: it is capable of reading all the objects in an iterable.<br><\/p>\n\n\n\n<p>We\u2019re going to create an iterator that iterates over a <a href=\"https:\/\/careerkarma.com\/blog\/python-array\/\">Python array<\/a>. This array contains a list of cakes.<br><\/p>\n\n\n\n<p>To stop our iterator from repeating infinitely, we\u2019re going to keep track of how many values are inside our iterable, and how many times our iterator has executed. This will allow us to stop our iterator from executing once it\u2019s iterated through all the items in an iterable.<\/p>\n\n\n\n<p>Open up a new Python file and paste in the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class Cakes:\n\tdef __init__(self, value):\n\t\tself.value = value\n\t\tself.max = len(value)\n\t\tself.count = 0\n\tdef __iter__(self):\n\t\treturn self\n\tdef __next__(self):\n\t\tif self.count &lt; self.max:\n\t\t\tto_return = self.value[self.count]\n\t\t\tself.count += 1\n\t\t\treturn to_return\n\t\telse:\n\t\t\traise StopIteration<\/pre><\/div>\n\n\n\n<p>This iterator is a bit more complicated than our last one. That\u2019s why we built an infinite iterator first. In this iterator, we have initialized three values:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>value: The item over which we want to iterate.<\/li>\n\n\n\n<li>max: The number of items in our iterable.<\/li>\n\n\n\n<li>count: The number of times our iterator has executed.<\/li>\n<\/ul>\n\n\n\n<p>In our <code>__next__()<\/code> method, we\u2019ve added an <a href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\">\u201dif\u201d statement<\/a>. This statement checks whether the number of times our iterator has run (count) is less than the number of items over which our iterable should iterate (max).<br><\/p>\n\n\n\n<p>If \u201ccount\u201d is less than \u201cmax\u201d, our iterator will find the next value in the iterable object. It will then add 1 to our \u201ccount\u201d variable so we can keep track of how many objects are left to iterate over. Then, we return the value that we calculated.<br><\/p>\n\n\n\n<p>It&#8217;s important to note that we assign a value to the variable \u201cto_return\u201d before we increment the \u201ccount\u201d variable. This ensures that we can access the value on the current iteration of our iterator.<br><\/p>\n\n\n\n<p>If we\u2019ve iterated over every item in our list, an exception will be raised called StopIteration. This halts our program once we\u2019ve iterated through every item in the list.<br><\/p>\n\n\n\n<p>Let\u2019s see if our iterator works. Add the following code to your main program and execute it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>cake = Cakes([\"Vanilla Sponge\"])\nfor c in cake:\n\tprint(c)<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Vanilla Sponge<\/pre><\/div>\n\n\n\n<p>Let\u2019s add in a few more items to our list and see what happens:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>cake = Cakes([\"Vanilla Sponge\", \"Carrot\", \"Coffee\"])\nfor c in cake:\n\tprint(c)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Vanilla Sponge\nCarrot\nCoffee<\/pre><\/div>\n\n\n\n<p>We\u2019ve done it! We\u2019ve successfully created an iterator that loops through our list of cakes. You deserve a cake \u2013 or even just a cupcake \u2013 for getting this far.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Iterators allow you to create objects that can be iterated upon. An iterator can loop through a list, a string, or another iterable object, and perform a particular action.<br><\/p>\n\n\n\n<p>Are you up for a challenge? Create an iterator that loops through a list of numbers and returns each number multiplied by two.<br><\/p>\n\n\n\n<p>The code for this iterator will be similar to our above example \u2013 it will still use the iterator protocol \u2013 but it should multiply each number in a list by two before it is returned to the main program.<br><\/p>\n\n\n\n<p>Now you\u2019re ready to start writing your own iterators in Python like an expert!<\/p>\n","protected":false},"excerpt":{"rendered":"How to Create a Python Iterator: A Guide Iterators are absolutely everywhere in Python; it\u2019s hard to learn even the basics without hearing the word \u201citerator\u201d or \u201citerable\u201d. An iterator is an object that can be iterated upon. While there are some built-in objects upon which you can iterate, you can also define your own.&hellip;","protected":false},"author":240,"featured_media":19356,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19355","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Iterator: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-iterator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Iterator: A Guide\" \/>\n<meta property=\"og:description\" content=\"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-iterator\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-11T18:33:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:54:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\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:\/\/careerkarma.com\/blog\/python-iterator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Iterator: A Guide\",\"datePublished\":\"2020-07-11T18:33:59+00:00\",\"dateModified\":\"2023-12-01T11:54:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/\"},\"wordCount\":1084,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-iterator\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/\",\"name\":\"Python Iterator: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg\",\"datePublished\":\"2020-07-11T18:33:59+00:00\",\"dateModified\":\"2023-12-01T11:54:29+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-iterator\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-iterator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Iterator: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Iterator: A Guide | Career Karma","description":"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.","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:\/\/careerkarma.com\/blog\/python-iterator\/","og_locale":"en_US","og_type":"article","og_title":"Python Iterator: A Guide","og_description":"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.","og_url":"https:\/\/careerkarma.com\/blog\/python-iterator\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-11T18:33:59+00:00","article_modified_time":"2023-12-01T11:54:29+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Iterator: A Guide","datePublished":"2020-07-11T18:33:59+00:00","dateModified":"2023-12-01T11:54:29+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/"},"wordCount":1084,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-iterator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/","url":"https:\/\/careerkarma.com\/blog\/python-iterator\/","name":"Python Iterator: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg","datePublished":"2020-07-11T18:33:59+00:00","dateModified":"2023-12-01T11:54:29+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Iterators are used to iterate through an object in Python. On Career Karma, learn how to write your own Python iterator.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-iterator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/maxwell-nelson-taiuG8CPKAQ-unsplash.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-iterator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Iterator: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19355"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19355\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19356"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}