Remove_duplicates


#1

numbers=[4,5,5,4]
numbers1=[]
def remove_duplicates(numbers):
for i in numbers:
if i not in numbers1:
numbers1.append(i)
return numbers1
print(remove_duplicates(numbers))

my code is perfectly right but python shows error while running this....I don't know whats the problem


#2

Hello ashim_code1,

there is just one little problem in your code.
the declaration of the empty list numbers1 =[] should be inside of your function.
I suppose that the list number1 has to be declared in the function, so that the list is empty each time your function is calles
I've tried it this way and it worked out.

numbers=[4,5,5,4]

def remove_duplicates(numbers):
    numbers1=[]
    for i in numbers:
        if i not in numbers1:
              numbers1.append(i)
     return numbers1
    
print(remove_duplicates(numbers))

#3
def remove_duplicates(lst): 
    numbers = []
    for n in lst:
        if n not in numbers:
            numbers.append(n)
    return numbers

#4

def remove_duplicates(lst):
lst = set(lst)
return lst


#5

Can anyone tell me what's wrong with my code?

def remove_duplicates(input_list):
copy_list = input_list
index = 0
while index < len(copy_list):
check_index = index + 1
while check_index < len(copy_list):
if copy_list[check_index] == copy_list[index]:
del copy_list[check_index]
check_index -=1
check_index += 1
index += 1
return copy_list

It shows that I have modified original list.


#6

@microwhiz17467 i didn't get it.
pls use
, , ,

your code here

, , ,

so we can see what you are trying t o say.
as per i can se, u deleted in copy_list which is equal to input_list, That's why it's showing you have modified the origina list


#7

wow, this is the first time i've written exactly the same as when is suggested in this post!

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

#8

sorry in advance, i'm still wondering how to indent my code.The easiest way is:

def remove_duplicates(x):
y=set(x)
z=list(y)
return z`


#9

this code below worked

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


#10

can anyone tell me whats wrong with my code....
def remove_duplicates(leet):
code=[]
code=leet[0]
for num in range(1,len(leet)):
for n in range(0,len(leet)):
if leet[num]!=code[n]:
code.append(leet[num])
return code


#12