Lazy lists in Python

homeblogtwitterthingiverse



Convert an iterator in Python into a lazily evaluated list. Surprisingly useful.

class Lazy_list:
    def __init__(self, iterator):
        self.data = [ ]
        self.iterator = iterator
    
    def __getitem__(self, index):
        while len(self.data) <= index:
            self.data.append( self.iterator.next() )
        return self.data[index]



[æ]