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.
An index refers to a position within an list.
Given a list and item, the task is to print all indexes of occurrences of that item in the list
- How to Create and Initialize a List of Lists in Python?
- Python Interview Questions and Answers for Freshers & Experienced
- Python Program to Check if a Number is Peterson Number
Examples:
Input :
givenlist = ["this", "is", "the", "new", "way", "to" , "learn" , "python", "which" , "is" ,"easy" ,"is" ] item = is
Output:
Indexes are : 1 9 11
Explanation:
Here "is" is present at the indices 1,9 and 11
Print the indices of occurrences of the item
There are several ways to find indices of item in list some of them are:
- Using For loop and list
- Using enumerate() function
- Using list comprehension and range
- Using count() function in itertools
- Using Numpy
Method #1: Using For loop and list
Create an empty list. Iterate through each element in the original list using a for-loop.
Append the index of each occurrence of the given item to the empty list.
Below is the implementation:
# Given list givenlist = ["this", "is", "the", "new", "way", "to", "learn", "python", "which", "is", "easy", "is"] # Given item item = "is" # Taking empty list indexes = [] # Loop over indices of elements in givenlist for index in range(len(givenlist)): # if element is equal to given item then append to empty list if givenlist[index] == item: indexes.append(index) # printing the result indexes print("Indexes are : ", *indexes)
Note : Here * is used to print list with spaces
Output:
Indexes are : 1 9 11
Method #2 : Using enumerate() function
The built-in function enumerate can be used to get the index of all occurrences of an element in a list ().
It was created to solve the loop counter issue and can be used in these type of problems.
Below is the implementation:
# Given list givenlist = ["this", "is", "the", "new", "way", "to", "learn", "python", "which", "is", "easy", "is"] # Given item item = "is" print("Indexes are :", end=" ") for index, element in enumerate(givenlist): # If the element is equal to item then print its index if element == item: print(index, end=" ")
Output:
Indexes are : 1 9 11
Method #3: Using list comprehension and range
You can get a list of all valid indices with the range() function and then compare the corresponding value
at each index with the given object.
Below is the implementation:
# Given list givenlist = ["this", "is", "the", "new", "way", "to", "learn", "python", "which", "is", "easy", "is"] # Given item item = "is" print("Indexes are :", end=" ") # Using list comprehension to store indexes in indexes list indexes = [index for index in range( len(givenlist)) if givenlist[index] == item] # print the list print(*indexes)
Output:
Indexes are : 1 9 11
Method #4 : Using count() function in itertools
We can use itertools count() function to build an iterator for efficient looping that returns evenly
spaced values beginning with the given amount.
You may use it in conjunction with the zip() function to add sequence numbers as shown below.
from itertools import count # Given list givenlist = ["this", "is", "the", "new", "way", "to", "learn", "python", "which", "is", "easy", "is"] # Given item item = "is" print("Indexes are :", end=" ") indexes = [(i, j) for i, j in zip(count(), givenlist) if j == item] # Traverse the indexes list and print only index for i in indexes: print(i[0], end=" ")
Note: Here we need only index so we print only index element in indexes list
Output:
Indexes are : 1 9 11
Method #5 : Using Numpy
We use numpy.where() function to get indexes of the occurrences of item in given list.
Below is the implementation
import numpy as np # Given list givenlist = ["this", "is", "the", "new", "way", "to", "learn", "python", "which", "is", "easy", "is"] # Given item item = "is" indexes = np.where(np.array(givenlist) == item)[0] print("Indexes are :",*indexes)
Output:
Indexes are : 1 9 11
Related Programs:
- python how to create a list of all the keys in the dictionary
- python how to create a list of all the values in a dictionary
- python how to get the list of all files in a zip archive
- python program to find the minimum index of a repeating element in an array list
- how to create and initialize a list of lists in python
- python how to check if an item exists in list
- python how to sort a list of tuples by second item