What would be the best way to find the index of a specified character in a list containing multiple characters?
+4
A:
>>> ['a', 'b'].index('b')
1
If the list is already sorted, you can of course do better than linear search.
AndiDog
2010-10-02 20:54:07
+1
A:
Probably the index
method?
a = ["a", "b", "c", "d", "e"]
print a.index("c")
Jim Brissom
2010-10-02 20:54:55
A:
As suggested by others, you can use index
. Other than that you can use enumerate
to get both the index
as well as the character
for position,char in enumerate(['a','b','c','d']):
if char=='b':
print position
Harpreet
2010-10-02 21:09:43