Looping over lists

kaens apatheticagnostic at gmail.com
Sat May 5 04:15:32 EDT 2007


I think the for i in range() is more readable (Maybe because I'm
coming from a c-style syntax language background) -  but what would
the benefits of using enumerate be (other that being more . . .
pythonesque?)

On 5/5/07, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Fri, 4 May 2007 19:26:17 -0700, aleax at mac.com (Alex Martelli)
> declaimed the following in comp.lang.python:
>
> > for i in range(n):
> >     for j in range(i+1, n):
> >         print a[i], a[j]
> >
>         Ah, but wouldn't the cleaner Python be something like
>
>
> >>> a = [1, 2, 3, 4, 5, 3, 6]   #just to confuse matters
> >>> for pos, val in enumerate(a):
> ...     for v2 in a[pos+1:]:
> ...             print val, v2
> ...
> 1 2
> 1 3
> 1 4
> 1 5
> 1 3
> 1 6
> 2 3
> 2 4
> 2 5
> 2 3
> 2 6
> 3 4
> 3 5
> 3 3
> 3 6
> 4 5
> 4 3
> 4 6
> 5 3
> 5 6
> 3 6
> >>>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfraed at ix.netcom.com           wulfraed at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-asst at bestiaria.com)
>                 HTTP://www.bestiaria.com/
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list