14/15 remove_duplicates


#1



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


my code works good, but I keep getting this error:
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!

even though I created 2 copies of the list handed to me, but I keep getting this error


to let me keep going, since I think the code is good, I also checked it in PyCharm


def remove_duplicates(a_list):
    new_list=a_list
    another_list=a_list
    for i,item in enumerate(another_list):
        for j,item_2 in enumerate(new_list):
            if item==item_2 and i != j:
                new_list.remove(item)
    return new_list


#2

this:

    new_list=a_list
    another_list=a_list

doesn't make a copy of a list, it creates a variable which refers to a list. use list() to create a new list:

new_list = list(a_list)

or you can create a loop and append all items to a new list, if you want to do it manually


#4

It doesn't work, I mean to do a list with the list() before hand.
and the problem is that my code works, as I said, I also checked it in Pycharm but it seems
that CodeAcademy have a problem with it.
you can see that it works even in the error message:

"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!"


#5

can you post an updated version of your code?

pyCharm doesn't count if you modify the original list, codecademy does care.


#7

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