Remove_duplicates


#1

def remove_duplicates(numbers):
    d={}
    l=[]
    for p1 in numbers:
        d[p1]=1
    for p2 in d:
        l.append(p2)
    return l

Why does it work ? Can someone explain the whole procedure please ?


#2

It works because dictionaries are sets of key/value pairs. A set cannot have any repeated values. consequently, with each assignment to the dictionary, any key which is already there is overwritten. The final dictionary has only unique keys. The second loop copies the keys to a list.

However, using a solution that we do not understand is a pretty good indication that it was borrowed from somewhere else and is not your original work. For the practice, see if you cannot come up with a solution of your own using a different approach. There are several ways to go about it.


#3

What I did was create a new variable and set it as set of the list. Then I returned the variable. That took 2 lines and worked for me


#4

Yes, but like the above it does not retain the order of the original sequence which a good solution will be able to do.


#5

A post was split to a new topic: It works but i'm not satisfyed


#6

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