Iteration index

Aahz Maruch aahz at
Fri Jun 1 01:37:54 CEST 2001

In article <3B169FEC.7AACB50E at>,
Paul Winkler  <slinkp23 at> wrote:
>I guess you could do it without the variable like this: 
>!! WARNING -- this only works if you use a list instead of a tuple
>AND you know that the list contains no duplicate items!!
>s = ['a', 'b', 'ala', 'a']
>for el in s:
>    print s.index(el), el

It's also extremely inefficient for large lists, because that's an
O(N^2) algorithm -- it has to scan the list to get the index.
                      --- Aahz  <*>  (Copyright 2001 by aahz at

Androgynous poly kinky vanilla queer het Pythonista
Hugs and backrubs -- I break Rule 6

"Characters exist to suffer, you know.  An author needs a little sadism
in her makeup."  --Brenda Clough

More information about the Python-list mailing list