Remove_duplicates


#1



I tested it in python IDLE and it is working.Why codeacademy do not accept it....?


def remove_duplicates(x):
    for number in x:
        count=0
        for number2 in x:
            if number==number2:
                count+=1 #it counts how many times it exists.
        for i in range(count-1):
            x.remove(number)#It removes item count times
    return x


#2

This is a problem that can be solved by printing interim values and watching what happens. I'll leave that with you for the time and we can revisit the question when you've given this a try.

Now for something right off the wall, but that will take advantage of list.remove()

def remove_duplicates(x):
    y = x[:]              # clone the list
    for n in y:           # iterate over the clone
        while n in x:     # loop through and remove all of `n`
            x.remove(n)
        x.append(n)       # append a single value of `n`
    return x

This is a rather weird solution, so you won't see it very often, if at all. Study it, though, and see if you don't gain some insight parallel to your own.


#3

Wow, it was simpler than I was thinking.Thank you for the quick response
I didn't know that i can do that:


#4

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