>>> ['a', 'b'].index('b')
1

If the list is already sorted, you can of course do better than linear search.

Answer from AndiDog on Stack Overflow
Discussions

Help with .index()? finding multiple instances of item
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself. More on reddit.com
๐ŸŒ r/learnpython
9
3
March 11, 2023
return all the indices where element is present in a python list - Stack Overflow
This return only the first index value where the char is present in the list. I need all the indices where char is present. def find_loc(char): for sub_list in chunks: if char in su... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Finding All Positions Of A Character In A String - Stack Overflow
I'm trying to find all the index numbers of a character in a python string using a very basic skill set. For example if I have the string "Apples are totally awesome" and I want to find the places ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 22, 2018
python - Find all the occurrences of a character in a string - Stack Overflow
@Goodword:This answer uses a list comprehension; see the Python tutorial. 2016-06-01T23:43:00.033Z+00:00 ... R. Wayne Over a year ago ยท The function is spelled wrong on the definition line. It should be "def findOccurrences" with two r's. 2018-02-04T00:20:05.663Z+00:00 ... import re example_string = "aaaaaa|bbbbbb|ccccc|dddd" indexes ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python find all indexes of a character in string
How to Find All Indexes of a Character in Python String | Delft Stack
February 2, 2024 - We defined the find(string, char) function that iterates through each character inside the string and yields the index i if the character matches char. While calling the find() function, we stored all the returned values inside a list and displayed ...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python find all indexes of character in a string | example code
Python find all indexes of character in a string | Example code
November 8, 2021 - You donโ€™t need to use a special method to find all indexes of characters in a string in Python. Just use for-loop and if statement logic to get done this task. Simple example code finds all occurrence index of โ€œsโ€ char in a given string. ...
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ python how to find index in a list: the index() function
Python How to Find the Index of Element in a List - codingem.com
July 10, 2025 - To find the index of a list element in Python, use the index() method of a list. For example, names.index("Bob") returns the index of "Bob".
๐ŸŒ
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)
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ get-index-of-character-in-python-list
Python: How to Get the Index of a Character in a List (Best Methods) โ€” pythontutorials.net
The enumerate() function is a Pythonic way to loop through a list while tracking both the index and the value of each element. It returns tuples of (index, value), making it easy to check for the target character.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-find-the-index-of-a-character-in-a-string-in-Python
How to find the index of a character in a string in Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 46320959 โ€บ return-all-the-indices-where-element-is-present-in-a-python-list
return all the indices where element is present in a python list - Stack Overflow
def find_loc(char): for sub_list in chunks: if char in sub_list: return chunks.index(sub_list), sub_list.index(char) ... Flagging for "Very low quality". Have a look at "How do I ask a good question?" Paco H. โ€“ Paco H. 2017-09-20 11:36:59 +00:00 Commented Sep 20, 2017 at 11:36 ... Index method returns the first index value encountered. ... index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value.
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
The index() method in Python finds the first occurrence of a specific character or element in a string or list. If the character or element is found, its index value will be returned.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-get-the-indices-of-all-occurrences-of-an-element-in-a-list
Python - Get the indices of all occurrences of an element in a list
March 27, 2026 - def get_indices(element, data): ... get_indices(element, my_numbers) print("Indices using index() method:", result) ... The enumerate() method with list comprehension is generally the most Pythonic and efficient approac...
๐ŸŒ
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 - There are a few ways to achieve this, and in this article you will learn three of the different techniques used to find the index of a list element in Python. ... Use the index() method to find the index of an item 1.Use optional parameters ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-find-indices-of-all-occurrences-of-character-in-string
Find all occurrences of a Substring in a String in Python | bobbyhadz
Use a list comprehension to iterate over the iterator. Use the match.start() method to get the indexes of the substring in the string.
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ python index: mastering list indexing techniques
Python List index() Method Explained with Examples
3 weeks ago - The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items.
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-find-index-containing-string-in-list
Python - Find Index containing String in List - GeeksforGeeks
July 23, 2025 - Let's explore more methods to find the index of a string in a list. ... next() method, combined with a generator expression, can be used to find the index of a string in a list.
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python: find an index (or all) of a substring in a string
Python: Find an Index (or all) of a Substring in a String โ€ข datagy
December 20, 2022 - Check out my in-depth tutorial about Python list comprehensions here, which will teach you all you need to know! Letโ€™s see how we can accomplish this using a list comprehension: a_string = "the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog" # Find all indices of 'the' indices = [index for index in range(len(a_string)) if a_string.startswith('the', index)] print(indices) # Returns: [0, 31, 45, 76]