
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
# 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)) # 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)) # 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)) # 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))
Output
Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3] Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3] Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3] Original list : [1, 3, 4, 3, 6, 7] New indices list : [1, 3]
- Related Articles
- Find elements of a list by indices in Python
- Find indices with None values in given list in Python
- Find the Maximum of Similar Indices in two list of Tuples in Python
- Program to find local peak element indices from a list of numbers in Python
- Python Program to get indices of sign change in a list
- Python program to remove elements at Indices in List
- Python – Character indices Mapping in String List
- Python program to get the indices of each element of one list in another list
- Python - Ways to format elements of given list
- Python - Ways to iterate tuple list of lists
- Get indices of True values in a binary list in Python
- Python - Ways to rotate a list
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries using values in python
- Different ways to clear a list in Python

Advertisements