Program - clone of orginal list with two functionality i. iterating and sequence


Topic: Program - clone of orginal list with two functionality i. iterating and sequence

Solution

class CustomListIter:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self):
        self.list_ = [1,2,3,4]
    def __len__(self):
        return len(self.list_)
    def __getitem__(self, i):
        if isinstance(i, int):
            if i<0:
                i = len(self.list_) + i
            if i<0 or i>=len(self.list_):
                raise IndexError('Invalid Input')
            else:
                return self.list_[i]
    def __iter__(self):
        return self.CustomListIterator(self)
    class CustomListIterator:
        def __init__(self, other):
            self.count = 0
            self.other = other
        def __iter__(self):
            return self
        def __next__(self):
            if self.count < len(self.other.list_):
                self.count += 1
                return self.other.list_[self.count]
            else:
                raise StopIteration



List all Python Programs