14/15 what am i doing wrong


#1

def remove_duplicates(origin_list):
item_list = origin_list
for item in item_list:
if item_list.count(item) > 1:
item_list.remove(item)
return item_list

the error message as below:
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!


#2

Maybe, one of solution is that you need to use list.append() to create new list.

def remove_duplicates(origin_list):
item_list = []
for item in origin_list:
item_list.append(item)
if item_list.count(item) > 1:
item_list.remove(item)
return item_list

Actually, It works.
Cheers


#3

Thank you, i did the same thing to figure out.
item_list = origin_list did not make a new list, i still need to learn more to understand it.
Thanks a lot.


#4

Here's a much simpler solution.
def remove_duplicates(text):
cleaned = []
for i in text:
if i not in cleaned:
cleaned.append(i)

return cleaned

#5

Please mark your Q/A as solved, thanks in advance :smile:


#6

I tried your solution and received the following output

Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [5, 5, 4] instead of [4, 5]

Did I miss something?


#7

the solution below worked for me:

def remove_duplicates(origin_list):
item_list = []
for item in origin_list:
item_list.append(item)
if item_list.count(item) > 1:
item_list.remove(item)
return remove_duplicates

The 'return item_list' needs to be replaced with 'return remove_duplicates'


#8

Why not this way?)

def remove_duplicates(l):
    new_list=set(l)
    return new_list

#9

in this lesson we did not learn how to use"set"