Alex Rivera | Logout

Position in an list?

Asked 2010-11-02T20:07:44.580
15

I will check if a word exists in a list. How can I show the position of this word?

Edit
Report

1 Answer

29
list = ["word1", "word2", "word3"]
try:
   print list.index("word1")
except ValueError:
   print "word1 not in list."

This piece of code will print 0, because that's the index of the first occurrence of "word1"

answered 2010-11-02T20:10:38.967

Your Answer