Remove_Duplicates using for loops, while loops and a count not working


#1



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


What I don't understand is why the removal process doesn't stop before the last copy of the digit is removed. I would think my while loop would handle that by exiting before it can remove the last copy but apparently it doesn't, or it loops too many times, or something. I'm muddled up and can't figure out where the logical errors are.

I peeked at the hint and saw there was a much easier way to do this exercise, but I'd like to understand why my method fails. Thanks!


def remove_duplicates(multiples):
    oncelist=[]
    for x in multiples:
        oncelist.append(x)
    count=0
    for x in multiples:
        print oncelist
        for y in oncelist:
            if x==y:
                count+=1
        print count
        while count >=1:
            for y in oncelist:
                if x==y:
                    count-=1
                    oncelist.remove(y)     
    return oncelist
remove_duplicates([3,3])
remove_duplicates([4,5,5,4])


#2

Removing from a list causes the following elements to move. If your loop doesn't account for that then you will skip elements.


#3

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