Why is this considered correct? I know I am missing something - what is it?


Exercise is remove_duplicates 14. in 15. practice makes perfect.

Codeacademy passes me on this exercise, but it only gives me the first number in the list, which occours more than once. It should however remove all numbers which occour more than once from the list.
Maybe I am taking the wrong approach here.?

def remove_duplicates(x):
    new_list = []
    for i in x:
        if x.count(i) > 1:
            return i
    return new_list.append(i)


Nvm, changed it to:

def remove_duplicates(x):
new_list = []
for i in x:
if i not in new_list:
return new_list

and now it does what it should. Still, the fact that it passes me on the code above is weird.


I was thinking the same,It should not be passed as It returns item of list.
As You have introduced the return statement inside the if clause ,It'll halt inside if-clause and the final list with duplicates removed will not be returned.


By the way, you can use three back-ticks to make your code look like the

Replace this line with your code

it makes your code easier to read and test


Alright, thanks for the tip!


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