tags:

views:

4471

answers:

2

For a list ["foo","bar","baz"] and a variable "bar", what's the cleanest way to get its index 1 in python?

+19  A: 
>>> ["foo","bar","baz"].index('bar')

1;

fivebells
+4  A: 

One thing that is really helpful in learning Python is to use the interactive help function:

>>> help(["foo", "bar", "baz"])
Help on list object:

class list(object)
 ...

 |
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value
 |

which will often lead you to the method you are looking for.

davidavr