remove_duplicate Why does it say 'list index out of range'?


I know there's a better way to do this, and I tried it, but I just want to know why this code doesn't work:

def remove_duplicates(li):
    cleared = []
    for i in li:#Create a new list, copy it from the old one, and sort it
    for i in range(0,len(cleared) - 2):
        if cleared[i] == cleared[i + 1]:#If two items next to each other are the same, remove one
            del cleared[i + 1]
    return cleared

Strangely, the code works all right if you pass in a list with < 5 items, such as remove_duplicate([1,1,2,3]), but once the length of the list is greater than 4, it gives me an error saying "List index out of range". Why is this happening?
PS: The error happens at cleared[i + 1]


Let's look at the problem, and you can relate it back to your code.

def remove_duplicates(lst):
    singles = []
    for i in lst:
        if i not in singles:
    return singles