Remove_duplicates


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#

Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [4] instead of [4, 5]


def remove_duplicates(first):
    import copy
    second =  copy.copy(first)
    fourth =  copy.copy(second)
    
           
    for number2 in second:
       second.remove(number2)
       third = copy.copy(second)
       for number4 in fourth:
           if number4 in third:
               fourth.remove(number4)
    return fourth


#2

@mukadam.khalid1gmail
May be you should read
http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python
to get an idear of what you are doing.....


#3

Hi,
I tried the other ways to copy the "first" list, but the same error appeared.

Thank you,


#4

I don't really like to just copy paste a solution, but there is a shorter and easier way to approach this problem.

Create an empty result list. Then iterate through the input. If the current number in the input list isn't in the result list, then append it.

It's good thinking using the copy library for creating copies of a list, but I'm not completely sure what you are trying to do in your solution.


#5

def remove_duplicates(first):
import copy
second = copy.copy(first)
fourth = copy.copy(second)

for number2 in second:                  loops through the list
   second.remove(number2)           removes one digit at a time
   third = copy.copy(second)           I'm not sure if I should do this.
   for number4 in fourth:                 loops through the original list    
       if number4 in third:                 if that digit is (still) in third, then that digit is there more than once 
           fourth.remove(number4)     and should be deleted
return fourth

#6

The solution I suggest looks something like

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

I'm looking around for a good solution more in line with your original thought, but I think the answer to this kind of problem simply is to build a new list rather than remove from an old list.