Remove_duplicates - Why does my code work when I call the function myself, but not when it is called automatically?


Hi all,

I'm stuck on the remove_duplicates exercise. What's incredibly strange to me is that I can manually supply any list, and my code performs as it should. However, when I submit my code, it returns something erroneous, and I can't figure out what's going on.

For example, when I submit my code, I will receive the following error message:

"Oops, try again. remove_duplicates([6, 8, 122, 2, 18]) returned [4, 5, 6, 8, 122, 2, 18] instead of [8, 122, 18, 2, 6]"

For good measure, try calling the function manually with any list that you want. For me, the function does as it should for all manually supplied lists. As soon as the function is called via saving the code, it will spit out something ridiculous.

The code I'm using is listed below

newlist = []
def remove_duplicates(n):
    for item in n:
        while newlist.count(item) == 0:
    return newlist

I realize that the method suggested in the hint is superior; however, I wanted to try and make it work my own way.


put the newlist declaration:

newlist = []

inside the function, the exercise calls your function multiply times with different lists to test how well it works, if newlist is not inside the function, a new function call won't clear the lists, causing problems


Makes sense.