Iteration over Lists and Strings

Andrew Durdin adurdin at
Sat Aug 28 05:38:26 CEST 2004

On Sat, 28 Aug 2004 03:22:48 GMT, DeepBleu <deepbleu at> wrote:
> What is going on?  Can someone clarify this to me?  And how can I ensure
> that the iteration produces the absolute index number **without** doing
> something like:
> >>a = 'abba'
> >>k = len(a)
> >>for m in range(0, k):
> >>  print a[m], m

The index() method looks up the first index of the given object in the
list/sequence. What you want is to use the enumerate() builtin:

>>>a = 'abba'
>>>for i, c in enumerate(a):
...    print c, i
a 0
b 1
b 2
a 3

More information about the Python-list mailing list