Lesson 14 remove_duplicates


#1



porfavor!
rodo o codigo ,vem aviso de erro dizendo que retorna [4,5,4] e no consoçeretornao reultado correto [4,5]
onde esta o erro!


b=[]
d=0
def remove_duplicates(c):
b.append(c[0])


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


[4, 5]


Replace this line with your code. 
b=[]
def remove_duplicates(c):
    b.append(c[0])
    for i in c:
        if i not in b:
           b.append(i)
        
    return b
    
print remove_duplicates([4,5,5,4])


#2

this line:

b=[]

should be inside the function, given the exercise (to test if your code is correctly) only calls the function, and not the whole script. Also, remove this line:

b.append(c[0])

because the exercise also checks for an empty list (which will cause problems), and your for loop can handle it


#3

este e o meu codigo,ele roda ok e volta o desejadp que e:

[4,5]

a=[4,5,5,4]
b=[]
def remove_duplicates(a):
b.append(a[0])
for i in a:
if i not in b:
b.append(i)
return b

print remove_duplicates(a)


#4

yes, but the exercise calls your function multiply times (only the function) so it should work for that, which is why you need to make the changes i suggested


#5

very good , thank!!!!!!!


#6

Did you resolve the issue?


#7

Hello guys,

I have successfully completed this exercise but feel like I shouldn't have.. Here is my code, what do you think ?

numbers = [1, 1, 2, 2, 3, 3]
duplicates = []
def remove_duplicates(numbers):
    for n in numbers[0:len(numbers)]:
        if n > 0: 
            duplicates.append(n)
    return n
print duplicates

#8

seems you shouldn't have passed, but it is impossible to eliminate all corner cases


#9

So I did it in what looks like a much more basic way than everyone else.

def remove_duplicates(x):
    new_list = []
    for i in x:
        if i not in new_list:
            new_list.append(i)
    return new_list    
    x = new_list    
    return x

Essentially I created a new list, and for each item that wasn't in said list I appended it.

I then set the parameter to equal this list and returned it. It worked on the first try.


#10

You code was great!
Effective but not really efficient, last 2 lines were redundant.


#11

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