[portland] Two Indices, One For Loop?

Matt McCredie mccredie at gmail.com
Tue Apr 22 00:43:17 CEST 2008


On Mon, Apr 21, 2008 at 3:24 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> On Mon, 21 Apr 2008, Matt McCredie wrote:
>
>
> > It's not clear to me what labelList and itemList look like. I'm assuming
> > something like this:
> >
> > labelList =
> ["label_0","label_1","label_2","label_3","label_4","label_5","label_6","label_7","label_8","label_9","label_10","label_11"]
> > itemList = [
> >   [ row0_item0, row0_item1, row0_item2, row0_item3, row0_item4,
> > row0_item5, row0_item6, row0_item7, row0_item8, row0_item9,
> > row0_item10, row0_item11,],
> >   [ row1_item0, row1_item1, ...],
> >   ....
> >   ]
> >
> > If that is the case, you can do something like this:
> >
>
>   That's the case, Matt.
>
>
>
> > for row in itemList:
> >  for label, item in zip(labelList, row):
> >    write(label)
> >    write(item)
> >  write("\n")
> >
>
>   That's essentially the same as regular nested for loops. Does it have
>  specific advantages?
>
>  Many thanks,
>
>
>
>  Rich

It doesn't use indecies so it would work even if you were using say
generator instead of lists or tuples. It is probably a _tiny_ bit
faster too, but I'm sure that isn't a concern. In general, I avoid
indecies if I can. Rarely should you need to call range(len(seq)), or
anything similar in python.

Matt


More information about the Portland mailing list