Use the buit-in index()
method from list
to find the index of an item in a list in Python.
Remember the index count starts at 0, so the position of ‘mclaren’ is 2.
car_brands = ['bmw', 'ferrari', 'mclaren']
position = car_brands.index('mclaren')
print(position)
2
index()
returns the position of the first occurrence of the given value.
In this example, the index returned is of the first appearance of ‘ferrari’, which is 1.
car_brands = ['bmw', 'ferrari', 'mclaren', 'ferrari']
position = car_brands.index('ferrari')
print(position)
1