[Tutor] Iterating over multiple lists- options

Shitiz Bansal shitizb at yahoo.com
Mon Feb 7 10:27:14 CET 2005


Hi,
My solution might raise purist's eyebrows but here it
goes...
How about using a try loop every time you read from
the list.

try:
   x=list[someno]
except:
   x=nothing(or whatever)

This goes on till the all lists start returning none.
for shorter lists try throws an index out of range
exception which is caught by except.

Shitiz
--- Tony Cappellini <tony at tcapp.com> wrote:

> 
> 
> I'm trying to generate an HTML table, from multiple
> lists.
> 
> There are 4 lists total, each of which *may* have a
> different length from 
> the other lists.
> Each list has been stored in a master dictionary.
> 
> 
> North=[Bill, Bob, Sue, Mary]
> South=['Tim', ''Tom', 'Jim', 'John', 'Carl', 'Evan',
> 'Rich']
> etc
> 
> d1={'North':North, 'South':South, 'East':East,
> 'West':West]
> 
> 
> I want to iterate over all the lists a the same
> time, so I can populate an 
> html table.
> This is approximately what the HTML table should
> look like, but the lists 
> can be in any order, top to bottom, and left to
> right.
> 
> South  North East West
> 
> Tim      Bill    May  Ellen
> Tom     Bob           Mick
> Jim      Sue           Ron
> John    Mary          Keith
> Carl                      Joey
> Evan
> Rich
> 
> 
> Looking through my books on Python I've found
> examples for zip() and map() 
> both of which have serious shortcomings
> That being, both of these functions can truncate the
> data, depending on 
> certain conditions
> 
> When iterating over multiple lists, it is fine if
> the mechanism returns an 
> empty string , or None for a non-existing list item.
> I just wont display anything in the HTML table for
> missing items.
> I know how to create the HTML table, statically. The
> problem is being able 
> to fill the table in one pass (preferably), which
> means my program would 
> need to iterate over
> more than one list at the same time.
> 
> Even using the range to generate an index has
> different effects, depending 
> on the order in which the lists are referenced in
> the for loop.
> 
> Are there any other options available for iterating
> over multiple lists ?
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 


More information about the Tutor mailing list