Looping over lists

Dustan DustanGroups at gmail.com
Sat May 5 07:07:00 EDT 2007


On May 5, 3:15 am, kaens <apatheticagnos... at gmail.com> wrote:
> 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?)

It doesn't create a whole new list just for iterating.

> On 5/5/07, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>
> > On Fri, 4 May 2007 19:26:17 -0700, a... 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
> >         wlfr... at ix.netcom.com           wulfr... at bestiaria.com
> >                 HTTP://wlfraed.home.netcom.com/
> >         (Bestiaria Support Staff:               web-a... at bestiaria.com)
> >                 HTTP://www.bestiaria.com/
> > --
> >http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list