Find Items in a Python List

To simply check if a list contains a particular item in Python you can use the in operator like this:

fruit = ['apple', 'banana', 'orange', 'lime']

if 'lime' in fruit:
    print('Lime found!')
# Lime found!

Find single index

To find index of an item in a Python list you can use the lists index() method like this:

fruit = ['apple', 'banana', 'orange', 'lime']
lime_pos = fruit.index('lime')

print(lime_pos)
# 3

Note that the above code prints out an index of 3 for lime because Python lists indices start at 0.

Find multiple indices

If you want to find the index for multiple items in a list then you can use the built-in enumerate() function to keep track of and store an index when a match is found:

fruit = ['apple', 'banana', 'orange', 'lime']
favourites = ['apple', 'lime']

# Find multiple indices with for loop
apples = []
for index, f in enumerate(fruit):
    if f in favourites:
        apples.append(index)
print(apples)
# [0, 3]

# Find multiple indices with list comprehension
apples = [index for index, f in enumerate(fruit) if f in favourites]
print(apples)
# [0, 3]

Filtering lists against lists

Bear in mind that when you use the in operator to search for an item within a list, Python will check each item in the list from first to last to find a match. For a small amount of searches this isn't really something to worry about. However, if you are filtering two relatively large lists against each other to make a third list this could result in a large amount of unnecessary computational effort and time. There may be a more efficient way to search for items.

If the list you're searching through does not need to contain duplicate items then you should consider using a Python set instead of a list. A set in Python is kind of like a list except a set cannot contain duplicate items and does not store the order of items. So why on earth would you want to use a set then?

Well, the advantage of sets is that they produce a hash for each item they contain. This means that when you search for an item in a set using the in operator Python will convert that into a hash and be able to determine if and where that item exists within the set in a single check. So if you are searching for many items within another large list and that list doesn't need to contain any duplicates then it's worth looking into using a set.

How much faster are sets that lists for searching? Well, when searching for 1000 items in a list of 100,000 items vs. a set of 100,000 items on a Macbook Pro this is how long each search took:

  • List: 4.129141 seconds

  • Set: 0.000150 of a second

Here is the code used to test the look up times:

You can also play with a repl of this code which has a much slower performance (around 14 seconds to search the list) presumably because it's running on limited resources.