14. duplicates


#1



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


I don't understand this error: "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 did make a copy and I'm returning the new_list, not the original. So not sure why I'm getting that error.


def remove_duplicates(dup_list):
    new_list = []
    for e in dup_list:
        dup_list.count(e)
        if 1 <= dup_list.count(e) > 1:
            dup_list.remove(e)
        for e in dup_list:
            new_list.append(e)
    return new_list


#2

here:

dup_list.remove(e)

you are still removing/modifying from the list that was passed into the function


#3

(already mentioned by Mod)
You need to create a separate copy of original list ..

# if x and y are lists. then by doing this..
#x and y will not point to same reference list object
x = y[:]

updated**
redundant code..
if 1 <= dup_list.count(e) > 1:


#4

Thanks to both of you. Here is my current code based on your feedback of not manipulating the original list:

def remove_duplicates(dup_list):
    new_list = []
    for e in dup_list:
            new_list.append(e) 
    for e in new_list:
        if new_list.count(e) > 1:
            new_list.remove(e)
    return new_list

Still haven't solved it, but will shortly. @greentreemee Is that if statement a mistake because you can't have more than one comparator like <, >, <=, ==, etc. in an if statement? Or was it something about the formatting?


#5

redundant code, as it can be done without it, just using new_list.count(e) > 1


#6

Finally figured it out and didn't need that anyway.

    new_list = []
    for e in dup_list:
        if e not in new_list:
            new_list.append(e)
    return new_list

Thanks again for the help.


#7