{"id":6278,"date":"2024-12-31T08:57:02","date_gmt":"2024-12-31T08:57:02","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6278"},"modified":"2024-12-31T09:05:20","modified_gmt":"2024-12-31T09:05:20","slug":"how-to-find-index-of-given-item-in-list-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/","title":{"rendered":"How to Find Index of Given Item in List in Python"},"content":{"rendered":"\n<p>Often software developers need to determine the index of a given element in Python lists. This is mainly required if you have a large list and want to search for one or more occurrences of a specific element. There are several simple ways to solve this problem using Python. In this article, we will learn how to find index of a given item in list in Python.<\/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-find-index-of-given-item-in-list-in-python\/#How_to_Find_Index_of_Given_Item_in_List_in_Python\" >How to Find Index of Given Item in List 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-find-index-of-given-item-in-list-in-python\/#1_Using_index_function\" >1. Using index() 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-find-index-of-given-item-in-list-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-find-index-of-given-item-in-list-in-python\/#3_Using_itertools\" >3. Using itertools<\/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-find-index-of-given-item-in-list-in-python\/#4_Using_range_function\" >4. Using range() 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-find-index-of-given-item-in-list-in-python\/#5_Using_numpy\" >5. Using numpy<\/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-find-index-of-given-item-in-list-in-python\/#6_Different_data_types\" >6. Different data types<\/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-find-index-of-given-item-in-list-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-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Find_Index_of_Given_Item_in_List_in_Python\"><\/span>How to Find Index of Given Item in List in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we proceed further, it is important to understand three critical use cases, when it comes to searching for an item in a list. First is when there is a single occurrence of a given item. Second, when there are multiple occurrences of given item. Third, when there is no occurrence of given item. In each of the following solutions, we will look at these use cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_index_function\"><\/span>1. Using index() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Every list supports an index() function that returns the index of the first occurrence of an element in the list. The index value starts from 0. If there are multiple occurrences of an item, it will still return the index of only the first item. If there are no occurrences, then it will throw a ValueError exception.<\/p>\n\n\n\n<p>Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list.index(x[, start[, end]])<\/pre>\n\n\n\n<p>In the above function, x is the item whose index you are looking for in the list. Start and end are optional arguments, to limit your search to a specific sublist of the given list. This is useful if you are dealing large lists, or you are sure to find an element in a specific sub-section of the given list.<\/p>\n\n\n\n<p>Let us say you have the following list<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = [1, 2, 3, 4, 5]<\/pre>\n\n\n\n<p>Here is the command to get index of item value=3 in the above list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(a.index(3)) # output is 2<\/pre>\n\n\n\n<p>Let us say you try to get the index of item=0 which does not exist in the list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(a.index(0))<\/pre>\n\n\n\n<p>You will get the following error message.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ERROR!<br>Traceback (most recent call last):<br>  File \"&lt;main.py&gt;\", line 2, in &lt;module&gt;<br>ValueError: 0 is not in list<\/pre>\n\n\n\n<p>To avoid this problem, it is better to wrap the index() function in a try-except block.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    print(a.index(0))<br>except ValueError:<br>    print('value not found')<\/pre>\n\n\n\n<p>Now when you run the above code, you will get the message &#8216;value not found&#8217; instead of the error.<\/p>\n\n\n\n<p>Let us say you have the following list with multiple occurrences of item=3.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = [1, 2, <strong>3<\/strong>, 4, 5, <strong>3<\/strong>]<\/pre>\n\n\n\n<p>If you use index() function, it will return only the index of first occurrence.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(a.index(3)) # output is 2<\/pre>\n\n\n\n<p>In this case, you need to use the next solution using enumerate() function.<\/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() function iterates over an iterable and automatically assigns an index for each item, which can then be retrieved. This makes it easy to access list items.<\/p>\n\n\n\n<p>There are two ways to use enumerate() function to get index of list item, whether there is single or multiple occurrence. Let us say you have the following list with 2 occurrences of item=3.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = [1, 2, 3, 4, 5, 3]<\/pre>\n\n\n\n<p>Here is the code to get indexes of item=3.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print([i for i, e in enumerate(a) if e == 3])<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[2, 5]<\/pre>\n\n\n\n<p>In the above code, we use a list comprehension, where we get a list of tuples as a result of enumerate() function. Each tuple consists of index, value combination. In our list comprehension, we loop through this list and in each iteration check if the value is 3. If so, we save the index in final list.<\/p>\n\n\n\n<p>The above solution works for most lists. However, if your list is really large and there are too many occurrences (say, millions) then the resultant list will occupy a lot of space. To overcome this problem, you can use a generator comprehension that returns an iterable object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">g = (i for i, e in enumerate(a) if e == 3)<\/pre>\n\n\n\n<p>In the above code, g is simply an iterator to a generator object and it does not occupy much space. You can access the individual indexes using next() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; next(g)<br>2<br>&gt;&gt;&gt; next(g)<br>5<\/pre>\n\n\n\n<p>In both these cases, if there is no occurrence of the item, then it will return an empty list, instead of throwing an error. So this method is quite comprehensive since it deals with no occurrence, single occurrence as well as multiple occurrences of an item.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_itertools\"><\/span>3. Using itertools<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Itertools is a popular Python library that provides tons of useful functions to work with iterable data such as lists, sets, etc. Among them, you can use izip() and count() functions to easily get indexes of one or more occurrences of an item in a list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = [1, 2, 3, 4, 5, 3]<br>from itertools import izip, count<br>print([i for i, j in izip(count(), a) if j == 3])<\/pre>\n\n\n\n<p>Output is [2, 5].<\/p>\n\n\n\n<p>Using itertools is faster than using enumerate() especially for larger lists, since it works using iterators and generator objects, which are time as well as memory efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_range_function\"><\/span>4. Using range() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use a simple range() function to get a list of indexes for a given list item.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">indexes = [i for i in range(len(a)) if a[i] == 3]<br>print(indexes)<\/pre>\n\n\n\n<p>In the above code, we use a list comprehension. In it, we use len() function to get the length of given list a. Then we use range() function to first get a list of indexes with length equal to that of given list. We iterate through this list of indexes and in each iteration, we check if the value of that index is equal to given item. If so, we store it in the result list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_numpy\"><\/span>5. Using numpy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Numpy<\/a> is a popular Python module that allows you to perform various operations on numbers. It is commonly used for its computational power, by scientists, data analysts and businesses. You can use its where() function to determine the index of a given item in a list. Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<br>a = [1, 2, 3, 4, 5, 3]<br>item = 3<br>np_array = np.array(a)<br>indexes = np.where(np_array==item)<br>print(indexes)<\/pre>\n\n\n\n<p>Here is the output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(array([2, 5]),)<\/pre>\n\n\n\n<p>In the above code, we first import Numpy library. Then we create a Numpy array from list a. Then we pass this array in where() function with the condition checking if item=3 exists in it. We store all result indexes in a result array.<\/p>\n\n\n\n<p>This is a very useful solution for large lists because Numpy arrays are more efficient than Python lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Different_data_types\"><\/span>6. Different data types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So far, we have seen examples where the original list contains only numbers and the item whose index we need, is also a number. What if the original list contains strings or dictionaries? All the above methods work for all those data types also. Let us say you have a list of strings, and you need to look for a string as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a  = ['hello', 'good', 'morning']<br>item = 'good'<\/pre>\n\n\n\n<p>Here are the above ways to get the index of list item using index() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(a.index(item))  # output is 1<\/pre>\n\n\n\n<p>Here we use enumerate() function along with list comprehension, exactly as we did in solution #2 above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print([i for i, e in enumerate(a) if e == item]) # output is [1]<\/pre>\n\n\n\n<p>Using izip() and count() function in itertools, we can get index of list item.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from itertools import izip, count<br>print([i for i, j in izip(count(), a) if j == item]) # output is [1]<\/pre>\n\n\n\n<p>You can also do the same thing using range() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">indexes = [i for i in range(len(a)) if a[i] == item]<br>print(indexes) # output is [1]<\/pre>\n\n\n\n<p>Here is how to do it using Numpy arrays.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<br>np_array = np.array(a)<br>indexes = np.where(np_array==item)<br>print(indexes) # output is (array([1]),)<\/pre>\n\n\n\n<p>You can also apply all the above solutions where your original list contains dict items and you need to get the index of another dict.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a  = [{1:1,2:2},{3:3,4:4}]<br>item = {1:1,2:2}<\/pre>\n\n\n\n<p>Here is an example to get index of list item using index() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(a.index(item)) # output is 0<\/pre>\n\n\n\n<p>Here is a demonstration of how enumerate() function can also be used to get index of dict in a list of dict.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print([i for i, e in enumerate(a) if e == item]) # output is [0]<\/pre>\n\n\n\n<p>As mentioned earlier, you can also use itertools for this purpose.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from itertools import izip, count<br>print([i for i, j in izip(count(), a) if j == item]) # output is [0]<\/pre>\n\n\n\n<p>You can also use range() function to get index of a dict in a list of dict.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">indexes = [i for i in range(len(a)) if a[i] == item]<br>print(indexes) # output is [0]<\/pre>\n\n\n\n<p>Lastly, here is how to do it using Numpy library.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<br>np_array = np.array(a)<br>indexes = np.where(np_array==item)<br>print(indexes) # output is (array([0]),)<\/pre>\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 how to find index of an item in Python list. They work with lists containing all sorts of data items &#8211; numbers, strings, dictionaries, etc. We have learnt how to find index using index() function. We also learnt how to use enumerate() and itertools along with list comprehensions for the same purpose. These methods work for single, multiple and no occurrence of item in list. We also looked at how to fix this problem using Numpy tools.<\/p>\n\n\n\n<p>If you are working with small lists where you know that your item exists, use index() function. If you are not sure about the presence of the item in list, or if there are multiple occurrences, use enumerate() function. If you are working with large lists, use itertools or Numpy library.<\/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 on lists with strings, numbers, dictionary, etc.?<\/strong><\/p>\n\n\n\n<p>Yes. They work with lists that contain numbers and strings.<\/p>\n\n\n\n<p><strong>2. Which solution to use for large lists?<\/strong><\/p>\n\n\n\n<p>If your list is large, then use enumerate() function with generator comprehension, or use itertools function izip() with count(). They both use iterator to a generator object, which is memory efficient.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/\">How to Flatten List of Lists in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\">How to Access Index Value in For Loop<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-directory-with-missing-parent-directory\/\">How to Create Directory with Missing Parent Directory<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to determine the index of a list item in Python. Here are the different ways to find index of given item in Python list.<\/p>\n","protected":false},"author":1,"featured_media":6317,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[304],"class_list":["post-6278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-index"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Find Index of Given Item in List in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.\" \/>\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-find-index-of-given-item-in-list-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 Find Index of Given Item in List in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-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-31T08:57:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-31T09:05:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"245\" \/>\n\t<meta property=\"og:image:height\" content=\"164\" \/>\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=\"7 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-find-index-of-given-item-in-list-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Find Index of Given Item in List in Python\",\"datePublished\":\"2024-12-31T08:57:02+00:00\",\"dateModified\":\"2024-12-31T09:05:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/\"},\"wordCount\":1400,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1\",\"keywords\":[\"index\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/\",\"name\":\"How to Find Index of Given Item in List in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1\",\"datePublished\":\"2024-12-31T08:57:02+00:00\",\"dateModified\":\"2024-12-31T09:05:20+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1\",\"width\":245,\"height\":164,\"caption\":\"get index of list item in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-find-index-of-given-item-in-list-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Find Index of Given Item in List 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 Find Index of Given Item in List in Python - Ubiq BI","description":"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.","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-find-index-of-given-item-in-list-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Find Index of Given Item in List in Python - Ubiq BI","og_description":"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-31T08:57:02+00:00","article_modified_time":"2024-12-31T09:05:20+00:00","og_image":[{"width":245,"height":164,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-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":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Find Index of Given Item in List in Python","datePublished":"2024-12-31T08:57:02+00:00","dateModified":"2024-12-31T09:05:20+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/"},"wordCount":1400,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1","keywords":["index"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/","name":"How to Find Index of Given Item in List in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1","datePublished":"2024-12-31T08:57:02+00:00","dateModified":"2024-12-31T09:05:20+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes you may need to find the index of a list item in Python. Here are the different ways to find index of given item in Python list.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1","width":245,"height":164,"caption":"get index of list item in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-find-index-of-given-item-in-list-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Find Index of Given Item in List 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\/get-index-of-item-in-list-in-python.jpg?fit=245%2C164&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1Dg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6278","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=6278"}],"version-history":[{"count":44,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6278\/revisions"}],"predecessor-version":[{"id":6327,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6278\/revisions\/6327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6317"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}