14. Remove_duplicates


#1



def remove_duplicates(numbers):
    singles = [] 
    duplicates = []
    numbers.sort()
    i = (len(numbers) - 1)
    while i > 0:
        if numbers[i] == numbers [i - 1]:
            duplicates.append(numbers[i])
        else:
           singles.append(numbers[i])
        i -= 1
    return singles

I am returning just the sorted list. I was expecting the while statement it iterate through the list starting with the last number. If the number directly behind it was the same after it was a duplicate then it goes to the duplicate list. If the number behind it is new it goes to the singles list. I have returned both the singles and duplicates lists at the end of the function and the are the same. I don't understand.



#2

You can verify whether it does that by printing out the value it's on at each iteration (what makes you say it doesn't? Or are you saying that it is?)

Perhaps there were as many of each for what you tested? Because that isn't always true.


#3

A post was split to a new topic: My remove_duplicates modifies the list


#5

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