How can I supress Error messages in python : So I'm trying to make a program to delete all duplicate Letters from a given String. The code is basically a selfmade iterator, that I got from another post here: <https://stackoverflow.com/questions/50427501/how-do-i-reset-a-list-iterator-in-python>


    
    string = "NNEEXXxxTTGGEENNCCOODDEERR"

    class rmvDuplicates:

    def __init__(self, list, newString):
        self.newString = newString
        self.list = list
        self.idx = 0
        self.content = list[self.idx]

    def __iter__(self):
        return self

    def get_content(self, content):
        return content

    def get_index(self):
        return self.idx

    def get_content_next(self, content):
        try:
            self.idx += 1
            return self.content
        finally:
            self.idx -= 1

    def remove(self, elem):
        del self.list[self.idx]
        return

    def rewind(self):
        self.idx = 0

    def __next__(self):
        try:
            return self.list[self.idx]
        except IndexError:
            pass
        finally:
            self.idx += 1
            self.content = self.list[self.idx]  

    new_String = []

    li = rmvDuplicates(list(string), new_String)

    for elem in li:
        if li.get_content(elem) == li.get_content_next(elem):
            print(li.get_content(elem))
            li.remove(elem)
            print(li.list)

    print("Hello")

I modified it a bit to match my needings and it works just fine, until it reaches the point of the IndexError.

    def __next__(self):
        try:
            return self.list[self.idx]
        except IndexError:
            pass
        finally:
            self.idx += 1
            self.content = self.list[self.idx]

I already tried to figure that out myself, using the Except-Block with the pass method.

But still, the program displays this message:

    Traceback (most recent call last):
      File "rmv_dplc_cls.py", line 49, in <module>
        for elem in li:
      File "rmv_dplc_cls.py", line 42, in __next__
        self.content = self.list[self.idx]
    IndexError: list index out of range

Does anyone here know how to fix this, so the program keeps on running and doesn't abort.

Thanks