{"id":2628,"date":"2023-10-16T15:35:37","date_gmt":"2023-10-16T10:05:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=2628"},"modified":"2023-11-10T11:43:22","modified_gmt":"2023-11-10T06:13:22","slug":"python-how-to-find-all-indexes-of-an-item-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-find-all-indexes-of-an-item-in-a-list\/","title":{"rendered":"Python How to Find all Indexes of an Item in a List"},"content":{"rendered":"
Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they’ve been created.<\/p>\n
An index refers to a position within an list.<\/p>\n
Given a list and item, the task is to print all indexes of occurrences of that item in the list<\/p>\n
Examples:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n There are several ways to find indices of item in list some of them are:<\/p>\n Create an empty list. Iterate through each element in the original list using a for-loop.<\/p>\n Append the index of each occurrence of the given item to the empty list.<\/p>\n Below is the implementation:<\/p>\n Note : Here * is used to print list with spaces<\/p>\n Output:<\/strong><\/p>\n The built-in function enumerate can be used to get the index of all occurrences of an element in a list ().<\/p>\n It was created to solve the loop counter issue and can be used in these type of problems.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n You can get a list of all valid indices with the range() function and then compare the corresponding value<\/p>\n at each index with the given object.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n We can use itertools count()<\/strong> function to build an iterator for efficient looping that returns evenly<\/p>\n spaced values beginning with the given amount.<\/p>\n You may use it in conjunction with the zip() function to add sequence numbers as shown below.<\/p>\n Note: Here we need only index so we print only index element in indexes list<\/p>\n Output:<\/strong><\/p>\n We use numpy.where() function to get indexes of the occurrences of item in given list.<\/p>\n Below is the implementation<\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. …<\/p>\ngivenlist<\/span>\u00a0=<\/span> [\"this\"<\/span>, \"is\"<\/span>,<\/span> \"the\",<\/span> \"new\", \"way\"<\/span>, \"to\" , \"learn\" , \"python\", \"which\" , \"is\" ,\"easy\" ,\"is\"<\/span>\u00a0]<\/span>\r\n\r\nitem<\/span>\u00a0= is<\/span><\/pre>\n
Indexes are : 1 9 11<\/pre>\n
Here \"is\" is present at the indices 1,9 and 11<\/pre>\n
Print the indices of occurrences of the item<\/h2>\n
\n
Method #1: Using For loop and list<\/h3>\n
# Given list\r\ngivenlist = [\"this\", \"is\", \"the\", \"new\", \"way\", \"to\", \"learn\", \"python\",\r\n \"which\", \"is\", \"easy\", \"is\"]\r\n\r\n# Given item\r\nitem = \"is\"\r\n\r\n# Taking empty list\r\nindexes = []\r\n\r\n# Loop over indices of elements in givenlist\r\nfor index in range(len(givenlist)):\r\n # if element is equal to given item then append to empty list\r\n if givenlist[index] == item:\r\n indexes.append(index)\r\n\r\n# printing the result indexes\r\nprint(\"Indexes are : \", *indexes)\r\n<\/pre>\n
Indexes are : 1 9 11<\/pre>\n
Method #2 : Using enumerate() function<\/h3>\n
# Given list\r\ngivenlist = [\"this\", \"is\", \"the\", \"new\", \"way\", \"to\", \"learn\", \"python\",\r\n \"which\", \"is\", \"easy\", \"is\"]\r\n\r\n# Given item\r\nitem = \"is\"\r\n\r\nprint(\"Indexes are :\", end=\" \")\r\n\r\nfor index, element in enumerate(givenlist):\r\n # If the element is equal to item then print its index\r\n if element == item:\r\n print(index, end=\" \")\r\n<\/pre>\n
Indexes are : 1 9 11<\/pre>\n
Method #3: Using list comprehension and range<\/h3>\n
# Given list\r\ngivenlist = [\"this\", \"is\", \"the\", \"new\", \"way\", \"to\", \"learn\", \"python\",\r\n \"which\", \"is\", \"easy\", \"is\"]\r\n\r\n# Given item\r\nitem = \"is\"\r\n\r\nprint(\"Indexes are :\", end=\" \")\r\n# Using list comprehension to store indexes in indexes list\r\nindexes = [index for index in range(\r\n len(givenlist)) if givenlist[index] == item]\r\n\r\n# print the list\r\nprint(*indexes)\r\n<\/pre>\n
Indexes are : 1 9 11<\/pre>\n
Method #4 : Using count() function in itertools<\/h3>\n
from itertools import count\r\n# Given list\r\ngivenlist = [\"this\", \"is\", \"the\", \"new\", \"way\", \"to\", \"learn\", \"python\",\r\n \"which\", \"is\", \"easy\", \"is\"]\r\n\r\n# Given item\r\nitem = \"is\"\r\n\r\nprint(\"Indexes are :\", end=\" \")\r\nindexes = [(i, j) for i, j in zip(count(), givenlist) if j == item]\r\n\r\n\r\n# Traverse the indexes list and print only index\r\nfor i in indexes:\r\n print(i[0], end=\" \")\r\n<\/pre>\n
Indexes are : 1 9 11<\/pre>\n
Method #5 : Using Numpy<\/h3>\n
import numpy as np\r\n# Given list\r\ngivenlist = [\"this\", \"is\", \"the\", \"new\", \"way\", \"to\", \"learn\", \"python\",\r\n \"which\", \"is\", \"easy\", \"is\"]\r\n\r\n# Given item\r\nitem = \"is\"\r\n\r\nindexes = np.where(np.array(givenlist) == item)[0]\r\n\r\nprint(\"Indexes are :\",*indexes)<\/pre>\n
Indexes are : 1 9 11<\/pre>\n
\n