14/15 Working Remove_Duplicate Code (do not know why)


#1

def remove_duplicates(lst):
for i in lst:
n = int(i in lst)
return n

I did this as part of an overall code, but the rest was unnecessary (not shown), since this returns the desired result. Can someone explain why?


#2

What you are doing here, is to check if the list element is in the list, which is obviously True. And then turn True into an integer, which is 1. :frowning:
At the very end you return the 1 (the integer you created by turning True into an integer).


#3

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


#4

It works for me.

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

#5

Thanks a lot.. Helped me understand the whole concept.


#6

Hey guys, what about this? trying to figure out why it returns 4 instead of 4 and 5

def remove_duplicates(l):
l_mod = []
for i in l:
l_mod.append(i)
return l_mod
for a in l_mod:
if l_mod.count(a) > 1:
l_mod.remove(a)
return l_mod


#7

Try this:

def remove_duplicates(l):
l_mod = []
for i in l:
l_mod.append(i)
for a in l_mod:
if l_mod.count(a) > 1:
l_mod.remove(a)
return l_mod

what i figured is in your first return

since you are still going to make use of the l_mod variable, i dont think there is any need returning it back to the function yet