{"id":6517,"date":"2023-03-06T04:39:29","date_gmt":"2023-03-06T04:39:29","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6517"},"modified":"2023-03-06T04:39:29","modified_gmt":"2023-03-06T04:39:29","slug":"explain-the-iterator-pattern-with-an-example-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/","title":{"rendered":"Explain the Iterator pattern with an example in Python"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable, which means we can iterate over them and they will return their member value one by one.<\/p>\n<p><em><strong>Some iterable objects are Lists, Tuples, and dictionaries.<\/strong><\/em><br \/>\nIterators in python are also objects, just like other things. It is used for iterating over iterable objects. This means we can use iterators on lists, tuples, dictionaries, and strings.<br \/>\nThey are implemented using two unique methods: iter () and next(). They are together called iterator protocol.<br \/>\nLet\u2019s learn how to use iterators<\/p>\n<h3>Using Python Iterators<\/h3>\n<p><strong>iter():<\/strong> This method is used to create the iterator over the iterable object. It returns the object of an iterator.<br \/>\n<strong>next():<\/strong> This method returns the next value from the object.<br \/>\nWe can use both methods to iterate over a variable.<\/p>\n<h3>Example:<\/h3>\n<p><code>mylist = ['This', 'is', 'Python' ,'List']\nmyIter = iter(mylist)<br \/>\nprint(myIter)<br \/>\n\/\/ output: &lt;list_iterator object at 0x7fd062b7f250><br \/>\nprint(next(myIter))<br \/>\n\/\/ output: This<br \/>\nprint(next(myIter))<br \/>\n\/\/ output: is<br \/>\nprint(next(myIter))<br \/>\n\/\/ output: python<br \/>\nprint(next(myIter))<br \/>\n\/\/ output: list<\/code><\/p>\n<p>In this example, we can see that printing myIter returns an iterator object.<br \/>\nOnce we have the iterator object we use next() to traverse through its values. Using this, we get all the values included in the python iterator.<br \/>\nIf we try to run next() again then we will get a StopIteration exception:<br \/>\n<em><strong>Traceback (most recent call last):<br \/>\n  File &#8220;\/home\/webner\/iter.py&#8221;, line 9, in &lt;module><br \/>\n\tprint(next(myIter))<br \/>\nStopIteration<\/strong><\/em><\/p>\n<p><small><em>People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/french-language-and-concepts-advanced\">French Certification Exams<\/a> to test the ability to communicate in the French language.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are<\/p>\n","protected":false},"author":1,"featured_media":6518,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[843,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Explain the Iterator pattern with an example in Python - SS Blog<\/title>\n<meta name=\"description\" content=\"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explain the Iterator pattern with an example in Python - SS Blog\" \/>\n<meta property=\"og:description\" content=\"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-06T04:39:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/03\/Iterator-pattern1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Explain the Iterator pattern with an example in Python\",\"datePublished\":\"2023-03-06T04:39:29+00:00\",\"dateModified\":\"2023-03-06T04:39:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\"},\"wordCount\":259,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Iterator pattern\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\",\"name\":\"Explain the Iterator pattern with an example in Python - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2023-03-06T04:39:29+00:00\",\"dateModified\":\"2023-03-06T04:39:29+00:00\",\"description\":\"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Explain the Iterator pattern with an example in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Explain the Iterator pattern with an example in Python - SS Blog","description":"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable","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:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Explain the Iterator pattern with an example in Python - SS Blog","og_description":"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable","og_url":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2023-03-06T04:39:29+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/03\/Iterator-pattern1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Explain the Iterator pattern with an example in Python","datePublished":"2023-03-06T04:39:29+00:00","dateModified":"2023-03-06T04:39:29+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/"},"wordCount":259,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Iterator pattern","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/","url":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/","name":"Explain the Iterator pattern with an example in Python - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2023-03-06T04:39:29+00:00","dateModified":"2023-03-06T04:39:29+00:00","description":"Everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. In python, some objects are iterable","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/explain-the-iterator-pattern-with-an-example-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Explain the Iterator pattern with an example in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":165,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6517"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=6517"}],"version-history":[{"count":1,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6517\/revisions"}],"predecessor-version":[{"id":6519,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6517\/revisions\/6519"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6518"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}