Python | Ways to find indices of value in list
Usually, we require to find the index, in which the particular value is located. There are many method 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 list.
Let’s discuss certain ways to find indices of value in given list.
Method #1 : Naive Method
We can achieve this task by iterating through the list and check for that value and just append the value index in new list and print that. This is the basic brute force method to achieve this task.
# Python3 code to demonstrate # finding indices of values # using naive method # initializing list test_list = [1, 3, 4, 3, 6, 7] # printing initial list print ("Original list : " + str(test_list)) # using naive method # to find indices for 3 res_list = [] for i in range(0, len(test_list)) : if test_list[i] == 3 : res_list.append(i) # printing resultant list print ("New indices list : " + str(res_list)) |
Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3]
Method #2 : 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 code to demonstrate # finding indices of values # using list comprehension # initializing list test_list = [1, 3, 4, 3, 6, 7] # printing initial list print ("Original list : " + str(test_list)) # using list comprehension # to find indices for 3 res_list = [i for i in range(len(test_list)) if test_list[i] == 3] # printing resultant list print ("New indices list : " + str(res_list)) |
Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3]
Method #3 : Using enumerate()
Using enumerate() we can achieve the similar task, this is slightly faster technique than above and hence is recommended to be used over the list comprehension technique.
# Python3 code to demonstrate # finding indices of values # using enumerate() # initializing list test_list = [1, 3, 4, 3, 6, 7] # printing initial list print ("Original list : " + str(test_list)) # using enumerate() # to find indices for 3 res_list = [i for i, value in enumerate(test_list) if value == 3] # printing resultant list print ("New indices list : " + str(res_list)) |
Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3]
Method #4 : Using filter()
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 code to demonstrate # finding indices of values # using filter() # initializing list test_list = [1, 3, 4, 3, 6, 7] # printing initial list print ("Original list : " + str(test_list)) # using filter() # to find indices for 3 res_list = list(filter(lambda x: test_list[x] == 3, range(len(test_list)))) # printing resultant list print ("New indices list : " + str(res_list)) |
Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3]
Recommended Posts:
- Python | Find indices with None values in given list
- Python | Find elements of a list by indices
- Python | Ways to find length of list
- Python | Indices of sorted list of list elements
- Python | Indices list of matching element from other list
- Python | Indices of N largest elements in list
- Python | Duplicate element indices in list
- Python | Group elements at same indices in a multi-list
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Python | Ways to sum list of lists and return sum list
- Python | Ways to Convert a 3D list into a 2D list
- Python | Ways to Spilt the list by some value
- Extending a list in Python (5 different ways)
- Python | Ways to flatten a 2D list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



