14/15 remove_duplicates My code words but CA returns illogical response


#1

The following code works in that it returns a de-duplicated list for any list I give it. The Codeacademy console shows a correct result with no errors, but in the code window displays the following illogical information:
Oops, try again. remove_duplicates([6, 8, 122, 2, 18]) returned [5, 4] instead of [8, 122, 18, 2, 6]

def remove_duplicates(mylist):
for item in mylist:
while clean.count(item) > 1:
clean.remove(item)
return clean

messy = [4, 5, 5, 4]
clean = messy

print remove_duplicates(messy)

Does CA recognize only one correct response? It's frustrating to write code that works but won't pass.

Thanks for any insights.

George.


#2

Well, it seems that your actual function is correct. But, as soon as the parser reads your script it apparently gets confused by your definiton of "messy" as a variable instead of directly handing it in as a parameter. It tries to solve with [6, 8, 122, 2, 18], encounters your "messy = [4, 5, 5, 4]" combined with "remove_duplicates(messy)" and alters the list. Then, obviously the result is [5,4] instead of [8, 122, 18, 2, 6].

Your code changed somewhat:

def remove_duplicates(mylist):
clean = mylist
for item in mylist:
while clean.count(item) > 1:
clean.remove(item)
return clean

print remove_duplicates([4, 5, 5,1,1]) #any list here

But here's the thing. It will now complain that you used the old list. But for the sake of this exercise it is forbidden to alter the original list and your code just renames it - "clean" is actually "mylist". Apparently you are supposed to fill a new list dynamically.

Dennis


#3

Here's my solution - a lot more lines, way less elegant, but it passed the parser:

`def remove_duplicates(mylist):
newlist = []
replace = False
for i in mylist:
if newlist == []:
newlist.append(i)
else:
for j in newlist:
if i == j:
replace = False
break
else:
replace = True
if replace == True:
newlist.append(i)
return newlist

print remove_duplicates([1,1,2,3,3,4,5,6,6,1])`


#4

Here is my solution. Tried to make it short and sweet.

def remove_duplicates(mylist):
    results = []
    for i in mylist:
        if i not in results:
            results.append(i)
    return results

print remove_duplicates([1,2,3,4,3,2,1,0,1,2,3,4,5])

#5

You're forcing your own input rather than allowing CA to test your code with their own inputs.