{"id":78278,"date":"2020-04-13T15:21:19","date_gmt":"2020-04-13T09:51:19","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78278"},"modified":"2020-04-13T15:21:19","modified_gmt":"2020-04-13T09:51:19","slug":"python-iterables","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/","title":{"rendered":"Python Iterables &#8211; Looping over Iterables in Python"},"content":{"rendered":"<p>You\u2019ll find <strong>iterables<\/strong> in almost every <strong>programming language<\/strong> you come across. These are important data types that you\u2019ll <strong>probably<\/strong> use in<strong> 99%<\/strong> of the <strong>programs<\/strong> you\u2019ll <strong>write<\/strong>. So the aim of this article is to get you <strong>acquainted<\/strong> with <strong>Python\u00a0 iterables<\/strong> and how you can work with them.<\/p>\n<p>We\u2019ll also introduce you to the<strong> built-in functions<\/strong> that <strong>operate<\/strong> on <strong>iterables<\/strong> to make your <strong>computational tasks<\/strong> easier. And stick along till the end of this article to learn some <strong>additional tricks<\/strong> on working with <strong>iterables<\/strong>.<\/p>\n<h3>What are Python Iterables?<\/h3>\n<p><strong>Iterables<\/strong>, as the name suggests, are <strong>objects<\/strong> which can be <strong>\u201citerated over\u201d<\/strong>.<\/p>\n<p>An iterable is simply a <strong>Python object<\/strong> which consists of a <strong>collection<\/strong> of <strong>data members<\/strong>. We can access the <strong>data members<\/strong> of an <strong>iterable<\/strong> one at a time. An iterable can be <strong>\u201citerated over\u201d<\/strong> with a <strong>loop<\/strong>, like a <strong>for loop<\/strong> in Python.<\/p>\n<p>Common examples of <strong>iterables<\/strong> include <strong>sequences<\/strong> like <strong>lists<\/strong>, <strong>tuples<\/strong> and <strong>strings<\/strong>. <strong>Non-sequential<\/strong> <strong>collections<\/strong> of data, like <strong>dictionaries<\/strong> and <strong>sets<\/strong>, are also examples of <strong>iterables<\/strong>.<\/p>\n<p>In the next section, we\u2019ll learn to use<strong> for loop<\/strong> to iterate over each of these <strong>iterables<\/strong>.<\/p>\n<h3>Looping over Iterables in Python<\/h3>\n<p>It\u2019s worth repeating ourselves: We can <strong>loop<\/strong> over <strong>iterables<\/strong> using a <strong>for loop<\/strong> in <strong>Python<\/strong>.<\/p>\n<p>Unlike other languages, Python\u2019s for <strong>loop doesn\u2019t require<\/strong> us to specify any <strong>start<\/strong> or <strong>stop<\/strong> <strong>indices<\/strong> to <strong>iterate<\/strong> over an <strong>iterable<\/strong>.<\/p>\n<p>We can simply use the syntax below:<\/p>\n<p><strong>for item in iterable:<\/strong><br \/>\n<strong>#do this<\/strong><\/p>\n<h3>1. for loop on lists:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; odd = [1, 3, 5, 7, 9]\n&gt;&gt;&gt; for item in odd:\nprint(item)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n3<br \/>\n5<br \/>\n7<br \/>\n9<\/div>\n<h3>2. for loop on tuples:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; odd = (1, 3, 5, 7, 9)\n&gt;&gt;&gt; for item in odd:\nprint(item)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n3<br \/>\n5<br \/>\n7<br \/>\n9<\/div>\n<h3>3. for loop on strings:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; name = \"Techvidvan\"\n&gt;&gt;&gt; for char in name:\nprint(char)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">T<br \/>\ne<br \/>\nc<br \/>\nh<br \/>\nv<br \/>\ni<br \/>\nd<br \/>\nv<br \/>\na<br \/>\nn<\/div>\n<h3>4. for loop on dictionaries:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; superhero = { 'Steve Rogers' : 'Captain America',\n'Tony Stark' : 'Iron Man',\n'Peter Parker' : 'Spiderman',\n'Bruce Banner' : 'Hulk'}\n&gt;&gt;&gt; for name in superhero:\nprint(name + \"'s superhero name is: \" + superhero[name])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Steve Rogers&#8217;s superhero name is: Captain America<br \/>\nTony Stark&#8217;s superhero name is: Iron Man<br \/>\nPeter Parker&#8217;s superhero name is: Spiderman<br \/>\nBruce Banner&#8217;s superhero name is: Hulk<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>5. for loop on sets:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; s = {1, 2, 4, 6}\n&gt;&gt;&gt; for item in s:\nprint(item)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n4<br \/>\n6<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Python Built-in functions that work on iterables<\/h3>\n<p>Let\u2019s now look at some <strong>commonly<\/strong> used <strong>Python built-in <\/strong>functions that work on <strong>iterables<\/strong> in <strong>Python<\/strong>. These <strong>functions<\/strong> prove useful while <strong>performing computational tasks<\/strong> on <strong>data elements<\/strong> of <strong>iterables<\/strong>.<\/p>\n<p>All these functions take an <strong>iterable<\/strong> as an <strong>argument<\/strong> and <strong>perform some operation<\/strong> on the <strong>elements<\/strong> of that <strong>iterable<\/strong>.<\/p>\n<p><strong>Function:<\/strong><\/p>\n<ul>\n<li><strong>sum()<\/strong><br \/>\nReturns <strong>sum<\/strong> of all the <strong>elements<\/strong> of an <strong>iterable<\/strong><\/li>\n<li><strong>sorted()<\/strong><br \/>\nReturns a <strong>list<\/strong> of <strong>elements<\/strong> of an <strong>iterable<\/strong> sorted in <strong>ascending<\/strong> order by default. You can sort in <strong>descending<\/strong> order by passing another argument <strong>\u201creverse = True\u201d<\/strong> to the <strong>sorted()<\/strong>.<\/li>\n<li><strong>max()<\/strong><br \/>\nReturns the element with the <strong>largest<\/strong> value in an iterable<\/li>\n<li><strong>min()<\/strong><br \/>\nReturns the element with the <strong>smallest<\/strong> value in an iterable<\/li>\n<\/ul>\n<h3>Working with Iterables &#8211; Python Tricks<\/h3>\n<p>While working with <strong>iterables<\/strong> in <strong>Python<\/strong>, you\u2019ll find these <strong>two tricks<\/strong> the most useful. These tricks will make your <strong>code<\/strong> more <strong>\u201cPythonic\u201d<\/strong>.<\/p>\n<h3>Iterable Unpacking<\/h3>\n<p>Iterable unpacking is a powerful feature while assigning values to multiple variables at the same time.<\/p>\n<p>Let\u2019s look at an example to get a better understanding.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; tup = (1, 2, 3)\n&gt;&gt;&gt; x, y, z = tup\n&gt;&gt;&gt; print(x, y, z)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1 2 3<br \/>\n&gt;&gt;&gt;<\/div>\n<p>Here <strong>x<\/strong>, <strong>y<\/strong> and <strong>z<\/strong> are the <strong>unpacking variables<\/strong>.<\/p>\n<p>The <strong>interpreter unpacks<\/strong> the <strong>tuple tup<\/strong> and <strong>assigns tup[0]<\/strong>, <strong>tup[1]<\/strong>,<strong> tup[2]<\/strong> to <strong>x<\/strong>, <strong>y<\/strong> and <strong>z<\/strong> respectively. <strong>Unpacking<\/strong> is also useful while <strong>iterating<\/strong> <strong>over<\/strong> an <strong>iterable<\/strong> of <strong>iterables<\/strong>.v<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; student = [(10, \"Archie\"), (11, \"Betty\"), (13,\"Veronica\")]\n&gt;&gt;&gt; for roll, name in student:\n     print(name, \"has the roll no.:\", roll)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Archie has the roll no.: 10<br \/>\nBetty has the roll no.: 11<br \/>\nVeronica has the roll no.: 13<br \/>\n&gt;&gt;&gt;<\/div>\n<p>Here, <strong>name<\/strong> and <strong>roll<\/strong> are <strong>unpacking variables<\/strong>.<\/p>\n<h3>Enumerating iterables<\/h3>\n<p><strong>Enumerating<\/strong> is useful when we need to keep a <strong>track<\/strong> of the <strong>iteration-count<\/strong> while <strong>looping over<\/strong> an <strong>iterable<\/strong>. We <strong>enumerate iterables<\/strong> using the<strong> built-in<\/strong> <strong>enumerate()<\/strong> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in enumerate(\"Techvidvan\"):\n     print(i)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(0, &#8216;T&#8217;)<br \/>\n(1, &#8216;e&#8217;)<br \/>\n(2, &#8216;c&#8217;)<br \/>\n(3, &#8216;h&#8217;)<br \/>\n(4, &#8216;v&#8217;)<br \/>\n(5, &#8216;i&#8217;)<br \/>\n(6, &#8216;d&#8217;)<br \/>\n(7, &#8216;v&#8217;)<br \/>\n(8, &#8216;a&#8217;)<br \/>\n(9, &#8216;n&#8217;)<br \/>\n&gt;&gt;&gt;<\/div>\n<p>The <strong>enumerate()<\/strong> <strong>function<\/strong> returns a <strong>new iterable<\/strong> that contains <strong>tuples<\/strong> of the <strong>iteration-count<\/strong> and the <strong>corresponding item<\/strong> from the <strong>original iterable<\/strong>.<\/p>\n<h3>Wrapping Up!<\/h3>\n<p>In this article, we learned what <strong>iterables<\/strong> are and <strong>how<\/strong> can we <strong>use<\/strong> them to <strong>structure<\/strong> and <strong>loop<\/strong> over our <strong>data<\/strong> in <strong>Python<\/strong>. Two major takeaways from this article are:<\/p>\n<ul>\n<li>You can <strong>loop<\/strong> over <strong>iterables<\/strong> using <strong>for loop<\/strong> in <strong>Python.<\/strong><\/li>\n<li><strong>Iterables may<\/strong> or <strong>may not<\/strong> have <strong>indices<\/strong>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019ll find iterables in almost every programming language you come across. These are important data types that you\u2019ll probably use in 99% of the programs you\u2019ll write. So the aim of this article is&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78282,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2403,2404,2405,2406,2407],"class_list":["post-78278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-enumerating-iterables","tag-iterable-unpacking","tag-looping-over-iterables","tag-python-iterables","tag-working-with-iterables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Iterables - Looping over Iterables in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Iterables - What is iterable in python with examples,looping over iterables in python, Iterables on built in functions, how to work with iterables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-iterables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Iterables - Looping over Iterables in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Iterables - What is iterable in python with examples,looping over iterables in python, Iterables on built in functions, how to work with iterables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-iterables\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-13T09:51:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Iterables - Looping over Iterables in Python - TechVidvan","description":"Python Iterables - What is iterable in python with examples,looping over iterables in python, Iterables on built in functions, how to work with iterables.","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:\/\/techvidvan.com\/tutorials\/python-iterables\/","og_locale":"en_US","og_type":"article","og_title":"Python Iterables - Looping over Iterables in Python - TechVidvan","og_description":"Python Iterables - What is iterable in python with examples,looping over iterables in python, Iterables on built in functions, how to work with iterables.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-13T09:51:19+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Iterables &#8211; Looping over Iterables in Python","datePublished":"2020-04-13T09:51:19+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/"},"wordCount":681,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg","keywords":["Enumerating iterables","Iterable unpacking","Looping over iterables","python iterables","Working with Iterables"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-iterables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/","url":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/","name":"Python Iterables - Looping over Iterables in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg","datePublished":"2020-04-13T09:51:19+00:00","description":"Python Iterables - What is iterable in python with examples,looping over iterables in python, Iterables on built in functions, how to work with iterables.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-iterables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-iterables.jpg","width":802,"height":420,"caption":"Python Iterables"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-iterables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Iterables &#8211; Looping over Iterables in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78278"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78278\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78282"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}