[Tutor] Iterating over multiple lists- options

Kent Johnson kent37 at tds.net
Mon Feb 7 12:03:50 CET 2005

Tony Cappellini 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

map(None, North, South, East West) does exactly what you want:
  >>> North=['Bill', 'Bob', 'Sue', 'Mary']
  >>> South=['Tim', 'Tom', 'Jim', 'John', 'Carl', 'Evan', 'Rich']
  >>> map(None, North, South)
[('Bill', 'Tim'), ('Bob', 'Tom'), ('Sue', 'Jim'), ('Mary', 'John'), (None, 'Carl'), (None, 'Evan'), 
(None, 'Rich')]

> That being, both of these functions can truncate the data, depending on 
> certain conditions

I don't think that is true for map(); what conditions are you thinking of?


More information about the Tutor mailing list