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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay