{"id":19678,"date":"2020-07-17T04:56:33","date_gmt":"2020-07-17T11:56:33","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19678"},"modified":"2023-12-01T03:55:29","modified_gmt":"2023-12-01T11:55:29","slug":"python-generator","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-generator\/","title":{"rendered":"How to Write a Python Generator"},"content":{"rendered":"\n<p>Iterators play an important role in Python. They allow you to create an object that can be iterated upon. It\u2019s a good way to store data that can be accessed through a for in loop.<br><\/p>\n\n\n\n<p>The trouble with iterators is that it takes a lot of work to build one. They are useful, but you have to write a lot of code to make one work. Generators help solve this problem.<br><\/p>\n\n\n\n<p>In this guide, we\u2019re going to talk about what Python generators are and why you should use them. We\u2019ll also implement a generator in Python to help you understand how they work.<br><\/p>\n\n\n\n<p>Without further ado, let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Generator?<\/h2>\n\n\n\n<p>A generator is a simple way of <a href=\"https:\/\/careerkarma.com\/blog\/python-iterator\/\">creating an iterator<\/a> in Python. It is a function that returns an object over which you can iterate.<br><\/p>\n\n\n\n<p>Generators are often called syntactic sugar. This is because they do not necessarily add new functionality into Python. They help make your code more efficient.<br><\/p>\n\n\n\n<p>Like iterators, you can only iterate over a generator once. This is because generators keep track of how many times they have been iterated upon, and cannot be reset.<br><\/p>\n\n\n\n<p>Let\u2019s take a look at a Python iterator:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class Cookies:\n\tdef __init__(self, value):\n\t\tself.value = value\n\n\tdef __iter__(self):\n\t\treturn self\n\n\tdef __next__(self):\n\t\treturn self.value<\/pre><\/div>\n\n\n\n<p>This code creates an iterator called Cookies. When you pass data through this iterator, it will be turned into an iterable object. This means that you can loop through the data using a for in loop. If you\u2019re looking at this code and think it is really long, that\u2019s because it is.<br><\/p>\n\n\n\n<p>Generators can help us shorten this code. Let\u2019s write a generator for our above iterator:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def cookies(value):\n\twhile True:\n\t\tyield value<\/pre><\/div>\n\n\n\n<p>Our code has been drastically reduced in size. Let\u2019s discuss how this works.<br><\/p>\n\n\n\n<p>A generator is a function that has a yield keyword instead of a return statement. Yield statements return a value from a function.<br><\/p>\n\n\n\n<p>The difference between a yield statement and a return statement is that a return statement stops a function from running, whereas a yield statement pauses the function and continues on iterating.<br><\/p>\n\n\n\n<p>Let\u2019s try to iterate over our simple generator function:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>for cookie in cookies(&quot;Raspberry&quot;):\n\tprint(cookie)\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p>Raspberry<\/p>\n\n\n\n<p>Raspberry<\/p>\n\n\n\n<p>\u2026<br><\/p>\n\n\n\n<p>This code keeps repeating until we stop our program. This is because we have used a <code>while<\/code> loop which executes forever. This isn\u2019t very useful in most cases. Let\u2019s write a generator that stops when an action has been performed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Write a Python Generator<\/h2>\n\n\n\n<p>We have a list of cookies that we want to print to the console. This list looks like this:<br><\/p>\n\n\n\n<p><code>[\u201cRaspberry\u201d, \u201cChoc-Chip\u201d, \u201cCinnamon\u201d, \u201cOat\u201d]<br><\/code><\/p>\n\n\n\n<p>To print these out to the console, we could create a simple generator. Open up a new Python file and paste in the following code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def print_cookies(cookies):\n\tlength = len(cookies)\n\tfor cookie in range(0, length):\nyield cookies[cookie]\n<\/pre><\/div>\n\n\n\n<p>Our generator goes through every cookie in the list we specify and returns each cookie individually. This generator does not work just yet. We have to use a for loop to iterate over it:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>cookie_list = [&quot;Raspberry&quot;, &quot;Choc-Chip&quot;, &quot;Cinnamon&quot;, &quot;Oat&quot;]\n\nfor c in print_cookies(cookie_list):\n\tprint(c)\n<\/pre><\/div>\n\n\n\n<p>We\u2019ve defined an array called <code>cookie_list<\/code> which stores a list of four cookies. We have then set up a for loop which uses our generator to iterate through the values in <code>cookie_list<\/code>.<br><\/p>\n\n\n\n<p>For each iteration in our for loop, the generated object is printed to the console:<br><\/p>\n\n\n\n<p>Raspberry<\/p>\n\n\n\n<p>Choc-Chip<\/p>\n\n\n\n<p>Cinnamon<\/p>\n\n\n\n<p>Oat<br><\/p>\n\n\n\n<p>We did it! This generator returns values until each value in the variable <code>cookie_list<\/code> has been iterated over.<br><\/p>\n\n\n\n<p>Generators have a range of use cases. Let\u2019s say that we have a list of order prices that we want to total up. We could do this using the following code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def total_orders(orders):\n\tlength = len(orders)\n\ttotal = 0\n\tfor o in range(0, length):\n\t\ttotal += orders[o]\n\t\tyield total<\/pre><\/div>\n\n\n\n<p>This generator will keep a running total of the value of all orders in a list. After each item has been iterated over, the generator will yield the current total value of the orders. Let\u2019s write a for loop which uses our generator:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>order_list = [2.30, 2.50, 1.95, 6.00, 7.50, 2.15]\n\nfor order in total_orders(order_list):\n\tprint(order)\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p>2.3<\/p>\n\n\n\n<p>4.8<\/p>\n\n\n\n<p>6.75<\/p>\n\n\n\n<p>12.75<\/p>\n\n\n\n<p>20.25<\/p>\n\n\n\n<p>22.4<br><\/p>\n\n\n\n<p>Our generator has calculated the cumulative value of all the orders in the order_list variable. Every iteration over the list returns the new cumulative value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Write a Generator Expression<\/h2>\n\n\n\n<p>Generators are already easier to write than an iterator. Our quest for writing cleaner code does not need to stop, thanks to generator expressions.<br><\/p>\n\n\n\n<p>Generator expressions are similar to <a href=\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\">list comprehensions<\/a>. Generator expressions produce one item at a time, like a generator. This is different from a list comprehension which produces an entire list, all at one time.<br><\/p>\n\n\n\n<p>Let\u2019s write a generator for our cookies example:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>cookie_list = [&quot;Raspberry&quot;, &quot;Choc-Chip&quot;, &quot;Cinnamon&quot;, &quot;Oat&quot;]\n\ncookie_generator = (cookie for cookie in cookie_list)\n<\/pre><\/div>\n\n\n\n<p>We\u2019ve defined a list of cookies in the variable <code>cookie_list<\/code>. We then create a generator expression. This expression uses the syntax for list comprehensions but with one big difference: list comprehensions are defined within square brackets, whereas generators are defined within rounded brackets.<br><\/p>\n\n\n\n<p>Let\u2019s create a for loop which iterates over the expression:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>for cookie in cookie_generator:\n\tprint(cookie)\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p>Raspberry<\/p>\n\n\n\n<p>Choc-Chip<\/p>\n\n\n\n<p>Cinnamon<\/p>\n\n\n\n<p>Oat<br><\/p>\n\n\n\n<p>The response of this generator is the same as the one from our first example. Our syntax is significantly clearer.<br><\/p>\n\n\n\n<p>It\u2019s important to note that when you iterate over a generator that was declared using a generator expression, you don\u2019t call it as a function. Our <code>cookie_generator<\/code> generator does not accept any input values: it already contains the code it needs to iterate over the <code>cookie_list<\/code> list.<br><\/p>\n\n\n\n<p>You should only use the generator expressions syntax when you need to write a generator that performs a simple function. Printing a list of values is a good example; multiplying values in a list by a specific number is another good example.<br><\/p>\n\n\n\n<p>This is because while the generator expression syntax is clear, it\u2019s mainly designed for one-line expressions. If your generator will use more than one line of code, write it as a generator function using the syntax we discussed earlier in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Are Generators Used?<\/h2>\n\n\n\n<p>The primary reason generators are used is that they are more concise than iterators.<br><\/p>\n\n\n\n<p>Generators are like a function and do not need any __init__, __iter__, or __next__ statements to work. This is unlike an iterator which requires all three of those statements. This behavior means that you can implement a generator in significantly fewer lines of code than you could if you were writing an iterator.<br><\/p>\n\n\n\n<p>What\u2019s more, generators are efficient at using memory. Generators only produce one item at a time. They don\u2019t create an entire sequence in memory before returning a result. This makes generators very practical if you need to iterate over a large list of data.<br><\/p>\n\n\n\n<p>The way this works is that iterators use a lazy evaluation method. They only generate the next element of an iterable object when that item is requested.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion (and Challenge)<\/h2>\n\n\n\n<p>Generators allow you to create a more Pythonic iterator.<br><\/p>\n\n\n\n<p>Generators make it easy to write objects using the iterator protocol without having to write __init__, __iter__, or __next__ statements. Generators are defined as functions. They use the yield statement to stop a generator and pass a value back to the main program before returning to its regular operations.<br><\/p>\n\n\n\n<p>If you\u2019re looking for a challenge, here are a few ideas:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Write a generator that reverses a string.<\/li><li>Write a generator that only returns values that contain a specific word from a list of values.<\/li><li>Write a generator that multiplies every number in a list by two.<\/li><\/ul>\n\n\n\n<p>For further reading, check out our tutorial on <a href=\"https:\/\/careerkarma.com\/blog\/python-iterator\/\">how to write and use Python iterators<\/a>. Now you\u2019re ready to start working with generators in Python like an expert!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"Iterators play an important role in Python. They allow you to create an object that can be iterated upon. It\u2019s a good way to store data that can be accessed through a for in loop. The trouble with iterators is that it takes a lot of work to build one. They are useful, but you&hellip;","protected":false},"author":240,"featured_media":19679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19678","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":null,"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>How to Write a Python Generator: A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python 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:\/\/careerkarma.com\/blog\/python-generator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Write a Python Generator\" \/>\n<meta property=\"og:description\" content=\"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python generator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-generator\/\" \/>\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-17T11:56:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:55:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"How to Write a Python Generator\",\"datePublished\":\"2020-07-17T11:56:33+00:00\",\"dateModified\":\"2023-12-01T11:55:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/\"},\"wordCount\":1229,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-generator\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-generator\/\",\"name\":\"How to Write a Python Generator: A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg\",\"datePublished\":\"2020-07-17T11:56:33+00:00\",\"dateModified\":\"2023-12-01T11:55:29+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python generator.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-generator\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg\",\"width\":1000,\"height\":667},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-generator\/#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\":\"How to Write a Python Generator\"}]},{\"@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":"How to Write a Python Generator: A Complete Guide | Career Karma","description":"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python 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:\/\/careerkarma.com\/blog\/python-generator\/","og_locale":"en_US","og_type":"article","og_title":"How to Write a Python Generator","og_description":"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python generator.","og_url":"https:\/\/careerkarma.com\/blog\/python-generator\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-17T11:56:33+00:00","article_modified_time":"2023-12-01T11:55:29+00:00","og_image":[{"width":1000,"height":667,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-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":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"How to Write a Python Generator","datePublished":"2020-07-17T11:56:33+00:00","dateModified":"2023-12-01T11:55:29+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/"},"wordCount":1229,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-generator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-generator\/","url":"https:\/\/careerkarma.com\/blog\/python-generator\/","name":"How to Write a Python Generator: A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg","datePublished":"2020-07-17T11:56:33+00:00","dateModified":"2023-12-01T11:55:29+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Generators allow you to write iterators in a more concise and Pythonic manner. On Career Karma, learn how to write a Python generator.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-generator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/sora-sagano-WFSap6CIXuw-unsplash.jpg","width":1000,"height":667},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-generator\/#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":"How to Write a Python Generator"}]},{"@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\/19678","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=19678"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19679"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}