Remove_duplicates: list index out of range error


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#

I'm stuck at the remove_duplicates exercise.I am getting the right output in the console but can't progress to the next stage


This is the piece of code that i have written. Is there any issues with the same?

def remove_duplicates(sequence):
    new_sequence=[]
    new_sequence.append(sequence[0])
    for i in sequence:
        if i not in new_sequence:
            new_sequence.append(i)
    return new_sequence


#2

do not assign the first item of sequence list to new_sequence
new_sequence.append(sequence[0])


#3

@fari123 is right, this line:

new_sequence.append(sequence[0])

causes the error, the lesson also test if your code works for an empty list. then the above line of code causes an error message (since there is no index 0 )

the for loop and if condition can perfectly handle it.


#4

thank you :slight_smile:


#5