15. remove_duplicates modifying argument



Error received:

Oops, try again. Your remove_duplicates modifies the list that was passed to it. 
[4, 5, 5, 4] was modified to [5, 4]. Perhaps you should make a copy first!

I don't see where the original argument (elements) is being modified, unless "unduped = elements" just creates unduped as a pointer to elements or something?

def remove_duplicates(elements):
    unduped = elements
    for u in unduped:
        while unduped.count(u) > 1:
    return unduped


yep, it did. This creates a variable which points to the same list in memory


Thanks! Is there a better way to copy a list into a new list than something like:

    unduped = []
    for e in elements:


well, there i list comprehension:

unduped = [x for x in elements]

which will be covered later, and there is the built in list function:

unduped = list(elements)

Those are the two alternatives i can come up with


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