DEV Community

I'm Just a Guy with a Computer
I'm Just a Guy with a Computer

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more