I don't know why this isnt working


#1


remove_duplicates

Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [4, 5, 4] instead of [4, 5]


def remove_duplicates(lst):
    
    one = []
    for index, i in enumerate(lst):
        if index == len(lst)-1:
            one.append(lst[index])
            break
        elif(lst[index] != lst[index + 1]):
            one.append(lst[index])
    
    return one


#2

not sure about the logic of this line:

elif(lst[index] != lst[index + 1]):

the list needs to remove all duplicate values, not successive duplicate values which i think this code does


#3

Well that line is supposed to check the current element and the element one
place ahead of it. If they are not the same that means that they are not
duplicates and it will go into the new list that will only contain originals


#4

but if we have the following list:

[1,2,3,1]

the result should be [1,2,3], your code wouldn't remove the 1 at the end, given they are not next to each other, even though it should


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.