{"id":196,"date":"2026-03-03T12:55:36","date_gmt":"2026-03-03T07:55:36","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=196"},"modified":"2026-03-22T18:19:35","modified_gmt":"2026-03-22T13:19:35","slug":"generators-and-iterators","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/","title":{"rendered":"Generators and Iterators"},"content":{"rendered":"\n<p>Generators and Iterators are tools in Python used to handle data efficiently, especially large datasets.<\/p>\n\n\n\n<p>They allow you to process data one item at a time instead of loading everything into memory.<\/p>\n\n\n\n<p>This improves:<\/p>\n\n\n\n<p>Memory efficiency<br>Performance<br>Scalability<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">What is an Iterator?<\/h1>\n\n\n\n<p>An Iterator is an object that allows you to traverse (loop through) a collection one element at a time.<\/p>\n\n\n\n<p>An object is an iterator if it has:<\/p>\n\n\n\n<p><strong>iter<\/strong>()<br><strong>next<\/strong>()<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3]<br>iterator = iter(numbers)print(next(iterator))<br>print(next(iterator))<br>print(next(iterator))<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>1<br>2<br>3<\/p>\n\n\n\n<p>When elements finish, it raises StopIteration.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">How Iterators Work<\/h1>\n\n\n\n<p>Iterator follows this process:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>iter<\/strong>() returns the iterator object<\/li>\n\n\n\n<li><strong>next<\/strong>() returns next value<\/li>\n\n\n\n<li>Raises StopIteration when done<\/li>\n<\/ol>\n\n\n\n<p>You can also create custom iterators.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Counter:<br>    def __init__(self, limit):<br>        self.limit = limit<br>        self.current = 0    def __iter__(self):<br>        return self    def __next__(self):<br>        if self.current &lt; self.limit:<br>            self.current += 1<br>            return self.current<br>        else:<br>            raise StopIteration<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">What is a Generator?<\/h1>\n\n\n\n<p>A Generator is a simpler way to create iterators using the yield keyword.<\/p>\n\n\n\n<p>Instead of writing <strong>iter<\/strong>() and <strong>next<\/strong>(), you use yield.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def count_up_to(limit):<br>    for i in range(1, limit + 1):<br>        yield igen = count_up_to(5)for num in gen:<br>    print(num)<\/pre>\n\n\n\n<p>Generators automatically handle StopIteration.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Yield vs Return<\/h1>\n\n\n\n<p>return:<\/p>\n\n\n\n<p>Ends function completely<\/p>\n\n\n\n<p>yield:<\/p>\n\n\n\n<p>Pauses function and remembers state<br>Resumes from where it left off<\/p>\n\n\n\n<p>This makes generators memory-efficient.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Generator Expressions<\/h1>\n\n\n\n<p>Similar to list comprehensions but use parentheses.<\/p>\n\n\n\n<p>List comprehension (stores all values):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [x*x for x in range(1000000)]<\/pre>\n\n\n\n<p>Generator expression (memory efficient):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = (x*x for x in range(1000000))<\/pre>\n\n\n\n<p>Generator expressions do not store full list in memory.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Why Generators Are Important<\/h1>\n\n\n\n<p>They:<\/p>\n\n\n\n<p>Use less memory<br>Handle large datasets efficiently<br>Improve performance<br>Support lazy evaluation<\/p>\n\n\n\n<p>Used in:<\/p>\n\n\n\n<p>Reading large files<br>Streaming data<br>Data pipelines<br>Big data processing<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Iterator vs Generator<\/h1>\n\n\n\n<p>Iterator:<\/p>\n\n\n\n<p>More code<br>Manual implementation<br>Requires <strong>iter<\/strong> and <strong>next<\/strong><\/p>\n\n\n\n<p>Generator:<\/p>\n\n\n\n<p>Short and simple<br>Uses yield<br>Automatically creates iterator<\/p>\n\n\n\n<p>Generators are easier and cleaner.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Real-World Example<\/h1>\n\n\n\n<p>Reading large file line by line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">with open(\"large_file.txt\") as file:<br>    for line in file:<br>        process(line)<\/pre>\n\n\n\n<p>File objects are iterators.<br>They read one line at a time, not entire file.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">When to Use Generators<\/h1>\n\n\n\n<p>Large datasets<br>Streaming data<br>Infinite sequences<br>Memory-sensitive applications<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Key Takeaway<\/h1>\n\n\n\n<p>Iterators allow sequential access to data.<br>Generators are a simple and memory-efficient way to create iterators using yield.<\/p>\n\n\n\n<p>They are powerful tools for handling large data efficiently in Python.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR DATA ENGINEERING (PYDE) > Working with Data at Scale > Generators and Iterators<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1774185523640\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":114,"template":"","class_list":["post-196","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Generators and Iterators - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generators and Iterators - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-22T13:19:35+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/generators-and-iterators\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/generators-and-iterators\\\/\",\"name\":\"Generators and Iterators - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T07:55:36+00:00\",\"dateModified\":\"2026-03-22T13:19:35+00:00\",\"description\":\"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/generators-and-iterators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/generators-and-iterators\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/generators-and-iterators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR DATA ENGINEERING (PYDE) > Working with Data at Scale > Generators and Iterators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Generators and Iterators - One Language. Endless Possibilities","description":"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.","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:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/","og_locale":"en_US","og_type":"article","og_title":"Generators and Iterators - One Language. Endless Possibilities","og_description":"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.","og_url":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-22T13:19:35+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/","url":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/","name":"Generators and Iterators - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T07:55:36+00:00","dateModified":"2026-03-22T13:19:35+00:00","description":"Learn Python iterators and generators to process large data efficiently, reduce memory usage, and improve performance with yield.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/generators-and-iterators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR DATA ENGINEERING (PYDE) > Working with Data at Scale > Generators and Iterators"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}