{"id":8813,"date":"2023-10-11T11:16:07","date_gmt":"2023-10-11T11:16:07","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8813"},"modified":"2023-10-11T11:16:07","modified_gmt":"2023-10-11T11:16:07","slug":"accessing-the-index-in-a-for-loops","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/accessing-the-index-in-a-for-loops","title":{"rendered":"Accessing the Index in a \u2018for\u2019 Loops"},"content":{"rendered":"<p>The most straightforward answer to this is to use an enumerate class. A built-in class called enumerate accepts an iterable object as input and outputs an object that may be typecast into a list of tuples.<br \/>\nThe syntax for the enumerate is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enumerate([\u201cApple\u201d, \u201cOrange\u201d, \u201cMango\u201d])\r\n\r\nOutput:\r\n[(0,\u201dApple\u201d), (1,\u201dOrange\u201d), (2, \u201cMango\u201d)]\r\n<\/pre>\n<p>So enumerate will give us a two dimensional (index, element) list and as we already know, using a for loop in Python, we can iterate through multidimensional lists as seen below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fruits = [\u201cApple\u201d, \u201cOrange\u201d, \u201cMango\u201d]\r\nfor index, fruit in enumerate(fruits):\r\n    print(index, fruit)\r\n<\/pre>\n<p><strong>Note:<\/strong> A further input argument called \u2018start\u2019 can be passed to enumerate. Start allows us to choose from where the index value should begin.<br \/>\n<strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enumerate([\u201cApple\u201d, \u201cOrange\u201d, \u201cMango\u201d], start=5)\r\n\r\nOutput:\r\n[(5,\u201dApple\u201d), (6,\u201dOrange\u201d), (7, \u201cMango\u201d)]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The most straightforward answer to this is to use an enumerate class. A built-in class called enumerate accepts an iterable object as input and outputs an object that may be typecast into a list of tuples. The syntax for the enumerate is as follows: enumerate([\u201cApple\u201d, \u201cOrange\u201d, \u201cMango\u201d]) Output: [(0,\u201dApple\u201d), (1,\u201dOrange\u201d), (2, \u201cMango\u201d)] So enumerate will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-8813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8813"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8813"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8813\/revisions"}],"predecessor-version":[{"id":8816,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8813\/revisions\/8816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8814"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}