15. remove_duplicates modifying argument


#1



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


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:
            unduped.remove(u)
            
    return unduped


#2

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


#3

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

    unduped = []
    for e in elements:
        unduped.append(e)

#4

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


#5

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