Remove_duplicates


#1



So I'm trying to see if appending the numbers to new_list then going through new_list for any duplicates and removing them, is easier or a right way of doing this. Is their an easy function that looks over a list and leaves one of each?


def remove_duplicates(lits):
    new_list = []
    for x in lits:
        new_list.append(x)
        for y in new_list:
        


Replace this line with your code.


#2

you can use in keyword to check if something is present in a list


#3

I came up with the soultion of the problem by mistake, but then I relized why it was so simple.

def remove_duplicates(x): # this is the defined function
dup_list=[] # this is the list that will keep all the charachers from x
for i in x: # this is looping into X
if i not in dup_list: # basically this says, if the number i is not in the dup_list add it, if it is do not add it.
dup_list.append(i)# this actually adds the number that is not already in the list.

return dup_list # this says ok at the end of the fuction return me the list that we created

EXAMPLE LIST:
n=[4,44,44,4,33,33,2,1]
print remove_duplicates(n)
[4, 44, 33, 2, 1]

I hope this helps to clarify the excersie, by the way I am learning so I do not know for sure if what I said make sense for you or anyone, so feedback is highly appreciated.


#4

def remove_duplicates(lits):
new_list = []
for x in lits:
if new_list==[]:
new_list.append(x)
continue
for y in new_list:
if x!=y:
new_list.append(x)

return new_list

please help me what is wrong with my code


#5

please stop hijacking other topics, go here and press the make new topic button and start making your own topics


#6

I'm a little confused here. I originally tried to return new_list but it wouldn't work. Replaced it with x and now it works. It also prints just the 2.

def remove_duplicates(lits):
    new_list = []
    for x in lits:
        if x not in lits:
            new_list.append(x)
    return x
    
print remove_duplicates([1,1,2,2])

#7

here:

if x not in lits:

why would you want to check if x is not in lits? all values are in lits, Please rethink this. You want to check if x is not yet in new_list, and then return new_list


#9