Why I think range is a wart.

Daniel Dittmar daniel.dittmar at sap.com
Wed Mar 13 12:26:40 EST 2002


> It happens to me too, mostly when I'm using a sliding window
> type algorithm, that is, I need to look at two adjacent members
> of the list. You can't do this easily by iterating through the list;
> unrolling the first entry and the last entry are a pain.
>
> Is there a way of handling this nicely? Somehow I doubt it.

class ListWindow:
    def __init__ (self, list):
        self.list = list + [None]   # assuming None is a suitable out of
bounds value

    def __getitem__ (self, index):
        return self.list [index:index + 2]

for currentItem, nextItem in ListWindow (mylist):
    <whatever>

Daniel





More information about the Python-list mailing list