Why is this one rejected ? It works when run with VS Code


No 14. Remove duplicates

Hello !
Can someone tell me why this one gets rejected ? It's working fine when I run it in with VS code, so I get confused.

Error message is =
Oops, try again. remove_duplicates([]) resulted in an error: list index out of range

Replace this line with your code. 
def remove_duplicates(list):
    list_s = list[:]
    answer = []
    for i in range(len(list_s) - 1):
        if list_s[i] != list_s[i + 1]:

    return answer


Hi, @designninja42610 ,

The error occurred when Codecademy tested your function by making this call, passing it an empty list ...


The list indexing in your code, for example, this line ...

if list_s[i] != list_s[i + 1]:

... assumes that the list that is passed to the function is not empty. You need to rewrite it so that it can handle lists of any length, including a length of 0.

There is a way you can write the function without using indexing, in case that helps.


Thanks a lot for your answer !

I never thought about an empty list. I added an IF to handle this case as step 1 in the function and got a pass.

To your point, I found the way to write the function without using indexing. I found it really quick (same way as suggested in the hints), so I thought I would try and find another extra solution. I got quite exited when I thought about using indexing in the original list as a basis for excluding duplicates because it appeared to me much more efficient when used on lists with a very large number of items.

Thanks a lot for your help!


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