Remove duplicates


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4

lists=['word','word',5,5,4,5,5,5]
def remove_duplicates(lists):   
        
    
    for i in lists:
         while lists.count(i)>1:
             lists.remove(i)
    return lists     
    print lists
           
       
       
print remove_duplicates(lists)


Oops, try again. Your remove_duplicates modifies the list that was passed to it. [4, 5, 5, 4] was modified to [5, 4]. Perhaps you should make a copy first!


the output is correct but i still get the error message!???


#2

print is not reachable after return.

Because of the way .remove() works, the first 4 is removed, then the first 5, leaving 5, 4. The SCT may be expecting the uniques to be in their original order with the second 4 removed, and the second 5, which would preserve the order. This suggests an entirely different approach.

The built-in function is a nice method, but doesn't offer us much practice with rudimentary algorithms. I'd give some of that a try before vacating this exercise.

As for tricking the SCT into accepting your code,

return sorted(lists)

but that is just a hack. Get in some practice with the basic code constructs learned in the first 8 modules.


#4

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