First create a dictionary containing in the index location of each item in the list (you state that all items are unique, hence no issue with duplicate keys).

Then use the dictionary to look up each item's index location which is average time complexity O(1).

my_list = ['ab', 'sd', 'ef', 'de']
d = {item: idx for idx, item in enumerate(my_list)}

items_to_find = ['sd', 'ef', 'sd']

>>> [d.get(item) for item in items_to_find]
[1, 2, 1]
Answer from Alexander on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-index-of-multiple-list-elements-in-python
Get Index of Multiple List Elements in Python - GeeksforGeeks
July 23, 2025 - In this example, in below code the `get_indices` function utilizes a list comprehension to directly generate a list of indices for elements in the `targets` list found in the original list `lst`. In the provided example, it prints the indices of 'apple' and 2. ... def get_indices(lst, targets): return [index for index, element in enumerate(lst) if element in targets] # Example usage: my_list = [1, 'apple', 3, 'banana', 2, 'orange'] target = ['apple', 2] result = get_indices(my_list, target) print(result)
🌐
Reddit
reddit.com › r/learnpython › help with .index()? finding multiple instances of item
r/learnpython on Reddit: Help with .index()? finding multiple instances of item
March 11, 2023 -

Taken from https://www.programiz.com/python-programming/online-compiler/?ref=409055e9 :

vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of the first 'i' is returned

index = vowels.index('i')

print('The index of i:', index)

Output: The index of i: 2

Say that the list was much bigger, and you don't know the contents, but you know 'i' is in it more than once. What would be the best way to find all instances of 'i'?

Thanks so much! <3

Top answer
1 of 5
5
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself.
2 of 5
3
str.index() has parameters start and end, which can be used to define the starting index for the search. So, if you know that you have more than one substring in your string, you can do the following: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = test.index('i') // i_idx -> 12 i_idx = test.index('i', i_idx+1) // i_idx -> 16 You can use it in the loop, but note that the str.index() throws an exception if the substring is not found. So, you can loop until you stop finding the substring in your string like this: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while True: try: i_idx = test.index('i', i_idx+1) print(i_idx) except ValueError as e: print(e) break Output: 12 16 42 45 61 65 substring not found There is a sibling function, str.find(). It works the same, but it does not raise exception, instead it returns -1 when the substring is not found, so you can loop until the result of str.find() is -1 to find indices of all substring occurrences in your string: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while (i_idx := test.find('i', i_idx+1)) > 0: print(i_idx) Output: 12 16 42 45 61 65 There is a function str.count() which can be used in a loop as well: i_idx = 0 for _ in range(test.count('i')): i_idx = test.index('i', i_idx+1) print(i_idx) The disadvantages of all the above method is that you have a time complexity of O(m*n) for a string with length n and m occurences of your substring in your string, with or O(n**2) for the worst case. Better time complexity can be achieved if you simply iterate through the string only once: for idx, char in enumerate(test): if char == 'i': print(idx) or using comprehensions generator expression (as pointed out by u/kyber/ : indices = (idx for idx, char in enumerate(test) if char == 'i') print(*indices)
Discussions

python - Find index for multiple elements in a long list - Stack Overflow
I have a very long lst containing unique elements. I want to design a function which takes a list of elements as the input and it can return a list of index efficiently. We assume the items needed to More on stackoverflow.com
🌐 stackoverflow.com
python - Access multiple elements of list knowing their index - Stack Overflow
I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]... More on stackoverflow.com
🌐 stackoverflow.com
python - finding index of multiple items in a list - Stack Overflow
I have a list myList = ["what is your name", "Hi, how are you", "What about you", "How about a coffee", "How are you"] Now I want to search index of all occurrence of "How" and "what".... More on stackoverflow.com
🌐 stackoverflow.com
python - How to find the indices of items in a list, which are present in another list? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
🌐
EyeHunts
tutorial.eyehunts.com › home › find index of multiple elements in list python
Find index of multiple elements in list Python
February 23, 2023 - my_list = ['ab', 'sd', 'ef', 'de'] d = {item: idx for idx, item in enumerate(my_list)} items_to_find = ['sd', 'ef', 'sd'] res = [d.get(item) for item in items_to_find] print(res) ... Use of the for Loop to Find the Indices of All the Occurrences of an Element. l1 = [1, 5, 1, 8, 9, 15, 6, 2, 1] pos = [] x = 1 for i in range(len(l1)): if l1[i] == x: pos.append(i) print(pos) ... Comment if you have any doubts or suggestions on this Python list topic.
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › get index of multiple list elements in python (3 examples)
How to Get Index of Multiple List Elements in Python (3 Examples)
June 19, 2023 - The for loop iterates over each element in the elements list and, for each of these elements, the index() method is applied on my_list to find the corresponding indexes in my_list.
🌐
freeCodeCamp
freecodecamp.org › news › python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List – How to Find the Index of an Item or Element in a List
February 24, 2022 - Use the index() method to find the index of an item 1.Use optional parameters with the index() method · Get the indices of all occurrences of an item in a list · Use a for-loop to get indices of all occurrences of an item in a list · Use ...
🌐
Delft Stack
delftstack.com › home › howto › python › find all indices of element in list python
How to Find All the Indices of an Element in a List in Python | Delft Stack
February 2, 2024 - A more efficient and compact way ... [i for i in range(len(l1)) if l1[i] == 1] print(pos) ... Similarly, we can also use the enumerate() function, which returns the index and the value together....
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - In this tutorial, you will learn about the Python index() function. The index() method searches an element in the list and returns its position/index. First, this tutorial will introduce you to lists, and then you will see some simple examples of how to work with the index() function.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Find the Index of an Item in a List in Python | note.nkmk.me
July 27, 2023 - def my_index_multi(l, x): return [i for i, _x in enumerate(l) if _x == x] print(my_index_multi(l, 10)) # [0, 2, 3] ... Refer to the following articles if you want to remove or extract duplicate elements from a list.
🌐
w3resource
w3resource.com › python-exercises › list › python-data-type-list-exercise-111.php
Python: Access multiple elements of specified index from a given list - w3resource
June 28, 2025 - Write a Python program to access multiple elements at a specified index from a given list. ... # Define a function 'access_elements' that extracts elements from a list based on their indices def access_elements(nums, list_index): # Use list comprehension to create a new list containing elements from 'nums' at the specified indices in 'list_index' result = [nums[i] for i in list_index] return result # Create a list 'nums' with integers nums = [2,3,8,4,7,9,8,2,6,5,1,6,1,2,3,4,6,9,1,2] # Print a message indicating the original list print ("Original list:") # Print the contents of the 'nums' list
🌐
Python Examples
pythonexamples.org › python-find-index-of-item-in-list
How to find index of an item in a list?
mylist = [21, 8, 67, 52, 8, 21, 87] ----------------- only this part of the list is considered for searching ^ index() finds the element here 0 1 2 3 4 => 4 is returned by index() A Python List can contain multiple occurrences of an element.
🌐
LabEx
labex.io › tutorials › python-how-to-find-multiple-indexes-efficiently-452156
How to find multiple indexes efficiently | LabEx
def find_indexes_with_enumerate(sequence, condition): return [index for index, value in enumerate(sequence) if condition(value)] numbers = [10, 20, 30, 20, 40, 20] even_indexes = find_indexes_with_enumerate(numbers, lambda x: x == 20) print(even_indexes) ## Output: [1, 3, 5] nested_list = [[1, 2], [3, 4], [2, 5], [1, 6]] target_first_element = 2 indexes = [index for index, sublist in enumerate(nested_list) if sublist[0] == target_first_element] print(indexes) ## Output: [2]
🌐
StrataScratch
stratascratch.com › blog › how-to-get-the-index-of-an-item-in-a-list-in-python
How to Get the Index of an Item in a List in Python - StrataScratch
September 6, 2024 - Let´s say that, in some cases, you must control how we find when an item does not exist in the list, or we want to locate all occurrences of this element, not only the first. ... Imagine you have a list of employee IDs and need to find an ID. You return a custom message instead of allowing your code to crash when the ID is absent. employee_ids = ['E001', 'E002', 'E003', 'E004', 'E005'] def find_index(item, lst): try: return lst.index(item) except ValueError: return f"Item {item} not found in the list" result = find_index('E003', employee_ids) print(result) result_not_found = find_index('E007', employee_ids) print(result_not_found)
🌐
datagy
datagy.io › home › python posts › python lists › python: find list index of all occurrences of an element
Python: Find List Index of All Occurrences of an Element • datagy
December 30, 2022 - The image below shows how these list indices work in Python: ... In the following section, you’ll learn how to use the list .index() method to find the index of an element in the list.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-the-indices-of-all-occurrences-of-an-element-in-a-list
Get the indices of all occurrences of an element in a list - Python - GeeksforGeeks
July 23, 2025 - List comprehension allows for a concise and efficient way to find indices. By iterating through the list enumerate() we can collect the indices where the element matches. The indices are stored in the list of indices.