Remove_duplicates


#1

Can someone please help me take a look what is wrong in my code. The message below keep popping up when I submit the code.

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

However, when I input [4, 5, 5, 4] into the function, I am getting the right answer returning [4, 5]. Thanks in advance!

def remove_duplicates(lst):
    lst.sort()
    new_list = []
    for i in range(len(lst)):
        if i == 0:
            new_list.append(lst[i])
        elif lst[i] != lst[(i-1)]:
            new_list.append(lst[i])
    return new_list

#2

for this exercise, you can't modify the list which you do here:

lst.sort()

by sorting the list, you modify it


#3

oh haha thxxx. I missed that instruction.


#4

what's wrong in the following code ?

def remove_duplicates(n=[]):
newlist=n[0]
for i in range(len(n)):
if n[i] not in newlist:
newlist = newlist.append(n[i])
else:
continue
return newlist

print remove_duplicates([1,1,2,2])


#5

newlist is a integer, it should be a list


#6

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