A non-slicky method:

def index_containing_substring(the_list, substring):
    for i, s in enumerate(the_list):
        if substring in s:
              return i
    return -1
Answer from kennytm on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-find-index-containing-string-in-list
Python - Find Index containing String in List - GeeksforGeeks
July 23, 2025 - It involves identifying the position where a specific string appears within the list. index() method in Python is used to find the position of a specific string in a 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 - Another way to find the indices of all the occurrences of a particular item is to use list comprehension. List comprehension is a way to create a new list based on an existing list. Here is how you would get all indices of each occurrence of the string "Python", using list comprehension: programming_languages = ["JavaScript","Python","Java","Python","C++","Python"] python_indices = [index for (index, item) in enumerate(programming_languages) if item == "Python"] print(python_indices) #[1, 3, 5]
Discussions

python - Get first list index containing sub-string? - Stack Overflow
For lists, the method list.index(x) returns the index in the list of the first item whose value is x. But if I want to look inside the list items, and not just at the whole items, how do I make the... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How can I find the index for a given item in a list? - Stack Overflow
Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Finding the index of the element contains string in a list - Stack Overflow
For example, the list contains: 2 strings 1 int 1 bool 1 nested list Example: ["string1", 34, True, "string2", [2,4,6]] Question: How to find the index of those 2 strings in the list? (object-type... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 6, 2015
python - Find all index position in list based on partial string inside item in list - Stack Overflow
mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"] I need the index position of all items that contain 'aa'. I'm having tr... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-find-index-containing-string-in-list
Python - Find Index Containing String in List
August 16, 2023 - mixed_list = [108, 'Safari', 'Skybags', ... isinstance() for robust type checking and list comprehension for concise code. The enumerate() function simplifies index tracking while iterating through lists....
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ get-list-index-python
Python Find String Position in List (Get the Index of an Item)
2 weeks ago - Use the index() method: my_list.index("target") returns the position of the first match, counting from 0. If the string might be missing, check with "target" in my_list first so you don't hit a ValueError. index() raises ValueError: 'x' is not ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
June 16, 2026 - Learn how to find a string in a Python list using in, index(), list comprehension, and regex. See practical, runnable code examples and start today.
Top answer
1 of 16
6115
>>> ["foo", "bar", "baz"].index("bar")
1

See the documentation for the built-in .index() method of the list:

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.

The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.

Caveats

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the beginning, this can slow down the code.

This problem can only be completely avoided by using a different data structure. However, if the element is known to be within a certain part of the list, the start and end parameters can be used to narrow the search.

For example:

>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514

The second call is orders of magnitude faster, because it only has to search through 10 elements, rather than all 1 million.

Only the index of the first match is returned

A call to index searches through the list in order until it finds a match, and stops there. If there could be more than one occurrence of the value, and all indices are needed, index cannot solve the problem:

>>> [1, 1].index(1) # the `1` index is not found.
0

Instead, use a list comprehension or generator expression to do the search, with enumerate to get indices:

>>> # A list comprehension gives a list of indices directly:
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> # A generator comprehension gives us an iterable object...
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> # which can be used in a `for` loop, or manually iterated with `next`:
>>> next(g)
0
>>> next(g)
2

The list comprehension and generator expression techniques still work if there is only one match, and are more generalizable.

Raises an exception if there is no match

As noted in the documentation above, using .index will raise an exception if the searched-for value is not in the list:

>>> [1, 1].index(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 2 is not in list

If this is a concern, either explicitly check first using item in my_list, or handle the exception with try/except as appropriate.

The explicit check is simple and readable, but it must iterate the list a second time. See What is the EAFP principle in Python? for more guidance on this choice.

2 of 16
725

The majority of answers explain how to find a single index, but their methods do not return multiple indexes if the item is in the list multiple times. Use enumerate():

for i, j in enumerate(['foo', 'bar', 'baz']):
    if j == 'bar':
        print(i)

The index() function only returns the first occurrence, while enumerate() returns all occurrences.

As a list comprehension:

[i for i, j in enumerate(['foo', 'bar', 'baz']) if j == 'bar']

Here's also another small solution with itertools.count() (which is pretty much the same approach as enumerate):

from itertools import izip as zip, count # izip for maximum efficiency
[i for i, j in zip(count(), ['foo', 'bar', 'baz']) if j == 'bar']

This is more efficient for larger lists than using enumerate():

$ python -m timeit -s "from itertools import izip as zip, count" "[i for i, j in zip(count(), ['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 174 usec per loop
$ python -m timeit "[i for i, j in enumerate(['foo', 'bar', 'baz']*500) if j == 'bar']"
10000 loops, best of 3: 196 usec per loop
Find elsewhere
Top answer
1 of 4
3

Use isinstance():

my_list = [True, 10.8, [1,2,3], False, True, "Hello", 12, "Sbioer", 2.5]

for i, item in enumerate(my_list):
    if isinstance(item, basestring):
        print i

Output:

5
7

However, if you want to check for int values, you will get bool-type item's indexes too because (quoting text from some other source):

It is perfectly logical, if you were around when the bool type was added to python (sometime around 2.2 or 2.3).

Prior to introduction of an actual bool type, 0 and 1 were the official representation for truth value, similar to C89. To avoid unnecessarily breaking non-ideal but working code, the new bool type needed to work just like 0 and 1. This goes beyond merely truth value, but all integral operations. No one would recommend using a boolean result in a numeric context, nor would most people recommend testing equality to determine truth value, no one wanted to find out the hard way just how much existing code is that way. Thus the decision to make True and False masquerade as 1 and 0, respectively. This is merely a historical artifact of the linguistic evolution.

So if you want to check for only int values just:

my_list = [True, 10.8, [1,2,3], False, True, "Hello", 12, "Sbioer", 2.5]

for i, item in enumerate(my_list):
    if isinstance(item, int) and not isinstance(item, bool):
        print i
2 of 4
0

You can use a combination of enumerate() and isinstance() to get the index of the string items:

l = [1, True, 'Hello', [1, 3, False, 'nested'], 'Bye']

for i, item in enumerate(l):
    if isinstance(item, basestring):   # Python 2. Use (str, bytes) instead of basestring for Python 3
        print i

Produces this output:

2
4

Note that this does not descend into the nested list; you will want to use recursion to do that.

๐ŸŒ
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 - Built-in Types - Common Sequence Operations โ€” Python 3.11.4 documentation ... To find the index of an item in a list, specify the desired item as an argument to the index() method.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-find-index-of-item-in-list
How to find index of an item in a list?
mylist = [21, 5, 8, 52, 21, 87, 52] item = 67 if item in mylist: index = mylist.index(item) print(f"index of {item} in the list is : {index}") else: print("item not present in list") ... In this Python Tutorial, we learned how to find the index of an element/item in a list using list.index() method...
๐ŸŒ
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
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Indexing in Python starts at 0, which means that the first element in a sequence has an index of 0, the second element has an index of 1, and so on. For example, if we have a string "Hello", we can access the first letter "H" using its index 0 by using the square bracket notation: string[0].
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're ...
Published ย  April 27, 2025
๐ŸŒ
Bomberbot
bomberbot.com โ€บ python โ€บ python-finding-the-index-of-a-string-in-a-list-a-comprehensive-guide
Python: Finding the Index of a String in a List - A Comprehensive Guide - Bomberbot
In this case, index() returns 2, indicating that 'cherry' is at the third position in the list (remember, Python uses zero-based indexing). However, it's important to note that index() has limitations. It only returns the index of the first occurrence of the string and raises a ValueError if the string is not found.
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to find the index for a given item in a list using python?
How to Find the Index of an Item in a List using Python?
April 7, 2026 - In Python, the index() method finds the index of a specified element in a list or string and returns its position. It returns the first occurrence index, which starts with 0. Otherwise, it raises a ValueError if the element is not found.