> Out of curiosity, if it's not possible to run zip() directly on the lists > that you have, can you bend the lists so that zip() will fit? It is possible, however zip() truncates the longer list, based on the size of the smaller list, so it's just not feasible in my case.
> Here's a quick function that should force a certain length on an iterator: > > ### > def ipad(iterable, length, sentinel=None): > """Returns a new iterator whose elements are taken from iterator. If > there are fewer elements than 'length', we pad the rest with > sentinels. > > Assumptions: len(iterator) <= length. The result from ipad never > truncates the elements out of i, so the iterator always goes through > all the elements in iterable. > """ > i = 0 > for thing in iterable: > yield thing > i = i + 1 > while i < length: > yield sentinel > i = i + 1 > ### > > > For example: > > ### > >>> names = ['knuth', 'mcconnell', 'bentley', 'witten'] > >>> for n in ipad(names, 7): > ... print n > ... > knuth > mcconnell > bentley > witten > None > None > None > >>> > >>> > >>> for n in ipad(names, 2): > ... print n > ... > knuth > mcconnell > bentley > witten > ### > > > So we could use something like ipad() to bring all the lists to the same > length, and that should make it suitable for zip(). > > > Hope this helps! > > I see- yes it does. And since it's my first use of generators, without really having to understand them, I'm more inclined to use this approach :-) I will just pad the short lists with the lenght of the longest list. However, this brings up a question about map() ing over lists of different lengths (which is what I started to use), but I'll post that in a different message. BTW, is your def ipad() function the 20gb, 40gb, or 60gb model ? :-) Thanks _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor