Iteration index

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


In article <3B169FEC.7AACB50E at yahoo.com>,
Paul Winkler  <slinkp23 at yahoo.com> 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 pobox.com)

Androgynous poly kinky vanilla queer het Pythonista   http://www.rahul.net/aahz/
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