Finding the Index of an Item in a Python list

You can use the index() function to find the index of an item in a list:

l = ['pike', 'halibut', 'monkfish', 'goldfish']
print l.index('halibut')

If the item is not in the list, a ValueError is raised, so you would need to check for this or handle it if you’re not sure.

if 'shark' in l:
    print l.index('shark')
    print 'Not there.'


    print l.index('shark')
except ValueError:
    print 'Not there.'