{"id":6083,"date":"2024-12-23T06:45:39","date_gmt":"2024-12-23T06:45:39","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6083"},"modified":"2024-12-23T08:41:08","modified_gmt":"2024-12-23T08:41:08","slug":"how-to-access-index-value-in-for-loop-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/","title":{"rendered":"How to Access Index Value in for Loop in Python"},"content":{"rendered":"\n<p>Software developers often employ different kinds of loops to iterate over data. While doing so, they sometimes need to get the index value of loop. There are several ways to access index value in for loop in Python. We will learn 8 different ways to retrieve the index value in a Python loop.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#How_to_Access_Index_Value_in_for_Loop_in_Python\" >How to Access Index Value in for Loop in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#1_Using_range_function\" >1. Using range() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#2_Using_enumerate_function\" >2. Using enumerate() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#3_Using_List_Comprehension\" >3. Using List Comprehension<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#4_Using_zip_function\" >4. Using zip() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#5_Using_counter_variable\" >5. Using counter variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#6_Using_yield_statement\" >6. Using yield statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#7_Using_lambda\" >7. Using lambda<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#8_Using_Pandas\" >8. Using Pandas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Access_Index_Value_in_for_Loop_in_Python\"><\/span>How to Access Index Value in for Loop in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us look at the different ways to access index value in for loop in Python. Let us say you have the following loop in Python.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = ['a','b','c','d','e']<\/pre>\n\n\n\n<p>Let us see different ways to get the index value while looping through the above list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_range_function\"><\/span>1. Using range() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Range() is a useful Python function that allows you to obtain a sequence of consecutive numbers starting from zero, until the specific number. We can loop through its result to obtain the index value for each item in the original list\/array.<\/p>\n\n\n\n<p>Here is the code to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in range(len(data)):<br>       print(i, data[i])<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(0, 'a')<br>(1, 'b')<br>(2, 'c')<br>(3, 'd')<br>(4, 'e')<br><\/pre>\n\n\n\n<p>In the above code, len() function returns the length of data list. Range() function returns a list of 5 consecutive numbers starting with 0. We loop through this list and in each iteration, use the list item as index to obtain the original data list&#8217;s element. In each iteration, we print the index and value.<\/p>\n\n\n\n<p>Please note, you can use this method on any iterable data, including strings. Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = \"Hello\"<br><br>&gt;&gt;&gt; for i in range(len(data)):<br>        print(i, data[i])<br><br>(0, 'H')<br>(1, 'e')<br>(2, 'l')<br>(3, 'l')<br>(4, 'o')<\/pre>\n\n\n\n<p>This is a very simple and intuitive solution to easily access index values in loops. They can also be used in while loops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_enumerate_function\"><\/span>2. Using enumerate() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Enumerate() is a useful Python function that adds a counter variable to an iterator, iterates through the iterable and returns pairs of index &amp; values. It is useful to easily get index, value pairs from iterables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = ['a','b','c','d','e']<br><br>&gt;&gt;&gt; for i in enumerate(data):<br>        print(i)<br><br>(0, 'a')<br>(1, 'b')<br>(2, 'c')<br>(3, 'd')<br>(4, 'e')<\/pre>\n\n\n\n<p>In the above code, enumerate() functions adds an index counter to the data list. Then in each iteration, it returns the pairs of index &amp; corresponding value from the list. You can also separately pick index and value from enumerate as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for index,value in enumerate(data):<br>        print(index,value)<br><br>(0, 'a')<br>(1, 'b')<br>(2, 'c')<br>(3, 'd')<br>(4, 'e')<\/pre>\n\n\n\n<p>The above approach allows you to easily access only the index or value as per your requirement, instead of working the enumerate tuple(index, value).<\/p>\n\n\n\n<p>By default, the starting index value of enumerate() function is zero. If you want, you can start it from another number by passing start option, as shown below. Here is an example to start enumerate() index from 2.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i,v in enumerate(data, start=2):<br>        print(i,v)<br><br>(2, 'a')<br>(3, 'b')<br>(4, 'c')<br>(5, 'd')<br>(6, 'e')<\/pre>\n\n\n\n<p>In the above code, the starting counter index is 2. <\/p>\n\n\n\n<p>Using enumerate() is generally favored by Python developers since it is very pythonic in nature. It is very fast, even if it appears as an overhead in the above code. So it can be used for large lists also.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_List_Comprehension\"><\/span>3. Using List Comprehension<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>List comprehension allows you to create a new list based on an existing list. This can be used to create a list of indexes for a given list. Here is a list comprehension to get a list of indexes based on data list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; print([i for i in range(len(data))])<br>[0, 1, 2, 3, 4]<\/pre>\n\n\n\n<p>We can use the elements of above list to get list elements from original list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; print([data[i] for i in range(len(data))])<br>['a', 'b', 'c', 'd', 'e']<\/pre>\n\n\n\n<p>You can also access the index as well as list values at the same time. Here we fetch the index and value pairs as tuples.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; print([(i,data[i]) for i in range(len(data))])<br>[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]<\/pre>\n\n\n\n<p>Some software developers find it difficult to figure out how list comprehensions work since we are basically cramming all the processing in a single line. Nevertheless, if you are comfortable with list comprehensions then they are definitely useful in this case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_zip_function\"><\/span>4. Using zip() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Zip() function allows you to combine 2 lists and iterate over both of them at once. For our purpose, we will use zip() function to combine and iterate over two lists &#8211; one with the indexes and the other with the values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = ['a','b','c','d','e']<br><br>indexes = [i for i in range(len(data))]<br><br>&gt;&gt;&gt; for index, value in zip(indexes, data):<br>        print(index, value)<br><br>(0, 'a')<br>(1, 'b')<br>(2, 'c')<br>(3, 'd')<br>(4, 'e')<br><\/pre>\n\n\n\n<p>Zip() function is super fast and works great even for large lists. However, you need to generate an additional list of indexes for this method. Otherwise, it is quite scalable, just like using enumerate().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_counter_variable\"><\/span>5. Using counter variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you are looking for an easy to understand solution, then this is one of the simplest ways to easily access index variable in loop. Just set a counter variable to initial index value, that is, zero. In each iteration, increment the counter by 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; for i in data:<br>        print(counter, i)<br>        counter+=1<br><br>(0, 'a')<br>(1, 'b')<br>(2, 'c')<br>(3, 'd')<br>(4, 'e')<\/pre>\n\n\n\n<p>Even beginner developers can easily use this solution. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Using_yield_statement\"><\/span>6. Using yield statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/what-does-yield-keyword-do-in-python\/\">Yield<\/a> statement is used to create generator function. It returns an iterator to a generator object. Every time, you access this iterator, it will return the next value in the sequence, without storing the entire sequence in memory. This is a useful method if you have a very large list that occupies lot of memory space.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def create_gen():        <br>    data = ['a','b','c','d','e']<br><br>    for (j, k) in enumerate(data):<br>        yield (j, k)<br>        <br><br>generator = create_gen()<br><br>for i in generator:<br>    print(i)<\/pre>\n\n\n\n<p>In the above code, we create a generator function that loops through a list using enumerat() function and returns an iterator to the generator object. We loop this generator object to get index-value pairs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Using_lambda\"><\/span>7. Using lambda<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.w3schools.com\/python\/python_lambda.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Lambda<\/a> are small, anonymous Python functions that accept one or more arguments but contain only a single expression. They cannot have multiple statements like a full fledged function. They are useful for writing compact code. Here is an example to access index values in a for loop using lambda functions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data = ['a','b','c','d','e']<br><br>&gt;&gt;&gt; x = lambda u:[(i, data[i]) for i in range(0, u)]<br>&gt;&gt;&gt; print(x(5))<br>[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_Using_Pandas\"><\/span>8. Using Pandas<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, your list may have unique indexes that cannot be accessed using enumerate() function. For example, if you have created Pandas series objects, then this will be the case. Let us say you have the following Series object with index as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br>xs = pd.Series([18, 13, 15])<br><br>xs.index = ['A123002', 'A123004', 'A123005']<\/pre>\n\n\n\n<p>If you use enumerate() function, this is what you get.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for id, x in enumerate(xs):<br>    print(\"item #{} = {}\".format(id, x))<br><br>#    item #0 = 18<br>#    item #1 = 13<br>#    item #2 = 15<\/pre>\n\n\n\n<p>Instead, you can use items() function available for each pandas series object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for id, x in xs.items():<br>    print(\"item #{} = {}\".format(id, x))<br><br>#    item #A123002 = 8<br>#    item #A123004 = 23<br>#    item #A123005 = 45<\/pre>\n\n\n\n<p>If you are using Pandas library or need to work with Series objects then you can use this method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt several different ways to access loop index in Python. You can use any of them as per your requirement. Even though we have demonstrated most of the solutions using for loop, you can also use it for other loop statements such as while loops. Among them, using enumerate() is one of the fastest ways to access indexes, and works well on large lists also.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. Do these solutions work for while loops?<\/strong><\/p>\n\n\n\n<p>Yes. They work for all kinds of loop statements in Python such as for loop, while loop, do&#8230;while, etc.<\/p>\n\n\n\n<p><strong>2. Do these methods also work for strings and other iterables?<\/strong><\/p>\n\n\n\n<p>Yes. They work on all iterables such as list, strings, etc. They also work on list of numbers, strings, tuples, and other data types.<\/p>\n\n\n\n<p><strong>3. Which is the most efficient of these solutions?<\/strong><\/p>\n\n\n\n<p>Among all the solutions provided above, using enumerate() is considered as the most efficient as well as most pythonic way of accessing index values.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-directory-with-missing-parent-directory\/\">How to Create Directory With Missing Parent Directory<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/\">Does Python Have Ternary Operator?<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-read-large-file-in-python\/\">How to Read Large File in Python?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often Python developers need to access index value in for loop. Here is how to get loop index in Python.<\/p>\n","protected":false},"author":1,"featured_media":6104,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[360],"class_list":["post-6083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-loop-index"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Access Index Value in for Loop in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Access Index Value in for Loop in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-23T06:45:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-23T08:41:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"301\" \/>\n\t<meta property=\"og:image:height\" content=\"201\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Access Index Value in for Loop in Python\",\"datePublished\":\"2024-12-23T06:45:39+00:00\",\"dateModified\":\"2024-12-23T08:41:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\"},\"wordCount\":1132,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1\",\"keywords\":[\"loop index\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\",\"name\":\"How to Access Index Value in for Loop in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1\",\"datePublished\":\"2024-12-23T06:45:39+00:00\",\"dateModified\":\"2024-12-23T08:41:08+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1\",\"width\":301,\"height\":201,\"caption\":\"access index value in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Access Index Value in for Loop in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Access Index Value in for Loop in Python - Ubiq BI","description":"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.","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:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Access Index Value in for Loop in Python - Ubiq BI","og_description":"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-23T06:45:39+00:00","article_modified_time":"2024-12-23T08:41:08+00:00","og_image":[{"width":301,"height":201,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Access Index Value in for Loop in Python","datePublished":"2024-12-23T06:45:39+00:00","dateModified":"2024-12-23T08:41:08+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/"},"wordCount":1132,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1","keywords":["loop index"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/","name":"How to Access Index Value in for Loop in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1","datePublished":"2024-12-23T06:45:39+00:00","dateModified":"2024-12-23T08:41:08+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often Python developers need to access index value in for loop. Here are different ways to get loop index in Python.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1","width":301,"height":201,"caption":"access index value in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Access Index Value in for Loop in Python"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/access-index-value-in-python.jpg?fit=301%2C201&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1A7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=6083"}],"version-history":[{"count":31,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6083\/revisions"}],"predecessor-version":[{"id":6117,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6083\/revisions\/6117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6104"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}