Python | Ways to find indices of value in list
Last Updated :
18 Aug, 2023
Usually, we require to find the index, in which the particular value is located. There are many methods to achieve that, using index(), etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in the list. Let’s discuss certain ways to find indices of value in the given list of Python.
Ways to find indices of value in the list
Below are the methods that we will cover in this article:
Find the Index of an Item Using the Naive Method
We can achieve this task by iterating through the list and checking for that value and just appending the value index in a new list and printing that. This is the basic brute force method to achieve this task.
Python3
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list : " + str (test_list))
res_list = []
for i in range ( 0 , len (test_list)):
if test_list[i] = = 3 :
res_list.append(i)
print ( "New indices list : " + str (res_list))
|
Output
Original list : [1, 3, 4, 3, 6, 7]
New indices list : [1, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Index of an Item Using List Comprehension
List comprehension is just the shorthand technique to achieve the brute force task, just uses lesser lines of codes to achieve the task and hence saves programmers time.
Python3
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list : " + str (test_list))
res_list = [i for i in range ( len (test_list)) if test_list[i] = = 3 ]
print ( "New indices list : " + str (res_list))
|
Output
Original list : [1, 3, 4, 3, 6, 7]
New indices list : [1, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Index of an Item Using Enumerate() Function
Using enumerate() we can achieve a similar task, this is a slightly faster technique than above and hence is recommended to be used over the list comprehension technique.
Python3
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list : " + str (test_list))
res_list = [i for i, value in enumerate (test_list) if value = = 3 ]
print ( "New indices list : " + str (res_list))
|
Output
Original list : [1, 3, 4, 3, 6, 7]
New indices list : [1, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Index of an Item Using filter() Function
This is yet another method that can be employed to achieve this particular task, filter() usually is able to perform the filtering tasks and hence can also be used in this situation to achieve this task.
Python3
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list : " + str (test_list))
res_list = list ( filter ( lambda x: test_list[x] = = 3 , range ( len (test_list))))
print ( "New indices list : " + str (res_list))
|
Output
Original list : [1, 3, 4, 3, 6, 7]
New indices list : [1, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Find the Index of an Item Using numpy Library
This program uses the numpy library to convert a given list into an array, finds the indices of the given value in the array, and converts the resulting numpy array back to a list. Finally, it prints the list of indices.
Python3
import numpy as np
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
test_array = np.array(test_list)
res_array = np.where(test_array = = 3 )[ 0 ]
res_list = list (res_array)
print ( "New indices list : " + str (res_list))
|
OUTPUT:
New indices list : [1, 3]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), because it creates a new numpy array with the same length as the input list.
Find the Index of an Item Using a for Loop
Initialize an empty list called “res_list” to store the indices of the target values.Iterate through each element in the input list “test_list” using a for loop.If the current element matches the target value, append its index to the “res_list”.After the loop is finished, return the “res_list” as the output.
Python3
test_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list: " + str (test_list))
res_list = []
for i in range ( len (test_list)):
if test_list[i] = = 3 :
res_list.append(i)
print ( "New indices list: " + str (res_list))
|
Output
Original list: [1, 3, 4, 3, 6, 7]
New indices list: [1, 3]
Time complexity: O(n), where n is the length of the input list “test_list”.
Auxiliary space: O(k), where k is the number of occurrences of the target value.
Find the Index of an Item Using list.index() Method with a while Loop
Initialize an empty list indexes to store the indices of the given value.Initialize a variable i to -1.Run a while loop that continues until the break statement is encountered.Inside the while loop, use the list.index() method to find the index of the given value in the list starting from index i + 1.If the index is found, append it to the indexes list and update the value of i to the index found.If the index is not found, break the while loop.Print the indexes list.
Python3
my_list = [ 1 , 3 , 4 , 3 , 6 , 7 ]
print ( "Original list : " + str (my_list))
indexes = []
i = - 1
while True :
try :
i = my_list.index( 3 , i + 1 )
indexes.append(i)
except ValueError:
break
print ( "New indices list : " + str (indexes))
|
Output
Original list : [1, 3, 4, 3, 6, 7]
New indices list : [1, 3]
Time Complexity: O(n),The list.index() method has a time complexity of O(n) in the worst case because it needs to iterate through the list to find the index of the given value.The while loop also has a time complexity of O(n) in the worst case because it needs to iterate through the list to find all occurrences of the given value.
Auxiliary Space: O(1),The space used by the indexes list and i variable is constant and does not depend on the size of the input list, so the auxiliary space complexity is O(1).
Please Login to comment...