[portland] Two Indices, One For Loop?
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 =
> > 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,
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.
More information about the Portland