Why I think range is a wart

Daniel Dittmar daniel.dittmar at sap.com
Thu Mar 14 12:53:32 EST 2002


> for item1, item2 in list1, list2
>    # lots of code here

class ZipOnTheFly:
    def __init__ (self, *lists):
        self.lists = lists
        self.elementSize = len (lists)

    def __getitem__ (self, index):
        result = [None] * self.elementSize
        for i in range (self.elementSize):
            result [i] = self.lists [i] [index]
        return result

for elem0, elem1 in ZipOnTheFly (list0, list1):
    <whatever>

This at least avoids some memory overhead if list0 and list1 are large.
Actually, the iterator based version looks cleaner (and doesn't iterate over
the indicees, which was the start of this thread)

def __init__ (self, *lists):
    self.iters = map (iter, lists)

def next (self):
    return [elem.next () for elem in self.iters]

Daniel






More information about the Python-list mailing list