DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

Finding a Value in a List with the index() Method in python.

A method is the same thing as a function, except it is “called on” a value.List values have an index() method that can be passed a value, and if that value exists in the list, the index of the value is returned. If the value isn’t
in the list, then Python produces a ValueError error. Enter the following into the interactive shell.

>>> teams = ['chelsea','RealMadrib','Barcelona','Juventus','Inter-Milano','Bayern-Munich']
>>> teams.index('Barcelona')
2
>>> teams .index('Manchester')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'Manchester' is not in list
>>> 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)