get index of character in python list
>>> ['a', 'b'].index('b')
1
If the list is already sorted, you can of course do better than linear search.
Probably the index
method?
a = ["a", "b", "c", "d", "e"]
print a.index("c")
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