{"id":9260,"date":"2024-01-10T04:40:40","date_gmt":"2024-01-10T04:40:40","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9260"},"modified":"2024-01-10T05:05:50","modified_gmt":"2024-01-10T05:05:50","slug":"how-to-access-the-index-value-in-a-for-loop","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/how-to-access-the-index-value-in-a-for-loop","title":{"rendered":"How to Access the Index Value in a &#8216;for&#8217; Loop?"},"content":{"rendered":"<p>You can use the enumerate function in Python to iterate over a sequence and simultaneously get the index and the value. Here&#8217;s how you can modify your code to achieve the desired output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">xs = [8, 23, 45]\r\nfor index, x in enumerate(xs, start=1):\r\n   print(\"item #{} = {}\".format(index, x))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the enumerate function in Python to iterate over a sequence and simultaneously get the index and the value. Here&#8217;s how you can modify your code to achieve the desired output: xs = [8, 23, 45] for index, x in enumerate(xs, start=1): print(&#8220;item #{} = {}&#8221;.format(index, x)) &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":9263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-9260","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\/9260"}],"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=9260"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9260\/revisions"}],"predecessor-version":[{"id":9262,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9260\/revisions\/9262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9263"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}