14/15 remove_duplicates - THere is any easy way?

Hey there, i did my code to create a new list without duplicate numbersvad 14/15 asks for, there is a short way of doing this?

Here is my solution, looking for shorter codes:


def remove_duplicates(dup_list):
no_dup = []
print dup_list
for duplicate in dup_list:
if duplicate not in no_dup:
no_dup.append(duplicate)
print no_dup
return no_dup

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


remove the prints :wink:

1 Like

yep, i just printed everything for debugging. So instead of prints, this is the shortest way, right?

@moscrow: The shortest possible code for this is:

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

Sets are “lists” with no duplicate elements so I think this code takes the prize.

12 Likes

Didn’t know about “set” element. Good to know!

@g4be you’re helping me a lot these days man, really appreciate your attention.

Thanks!

1 Like

@moscrow: I didn’t know about sets either but while looking around lists I found about sets so that could you with your challenge.

Glad to help!

P.S.: Just noticed you are from Brazil , why hello there fellow brazilian programmer!

1 Like

@g4be: Wow didn’t noticed you’re from Brazil too, that’s cool hahaha

Você ta me ajudando muito mesmo, sério, obrigado! :grin:

1 Like

Shorter code bro:

4 Likes

How about this

def remove_duplicates(source_list): 
    dest_list = []
    for element in source_list: 
        if element not in dest_list: 
            dest_list.append(element)
    return dest_list
2 Likes

def remove_duplicates(numbers):
new_list =
for x in numbers:
if x not in new_list:
new_list.append(x)

return new_list

print remove_duplicates([4,5,5,4])

1 Like

thanks everyone! its really good looking at different type of codes you guys develop, just adding to my experience and learning

1 Like

@g4be
Wouldn’t the shortest possible code be a lambda?
Example:

remove_duplicates = lambda s: list(set(s))
2 Likes

@cadecodes: And there’s lambda, ladies and gentleman!

yes,.
I did everything as above in my own,…but missed te structure of return stmt under if loop instead of above you mentioned ,.
thanks

Thought would have a go at doing this without using the “not in” functionality. The below worked:

i wrote the exact same code but i am getting a ‘builtin_function_or_method’ object has no attribute ‘getitem

This seems a touch leaner - no prints in my function, just used print to call the function so I could see the result in the console - but I think we’re both solving the problem the same basic way:
{my_list = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]

def remove_duplicates(dup):
new_list =
for i in dup:
if i not in new_list:
new_list.append(i)
return new_list

print remove_duplicates(my_list)}

Here’s a quickie for you:

def remove_duplicates(x):
new_list =
for items in x:
if items not in new_list:
new_list.append(items)
return new_list

Works for me!:grin:

is it cheating to use tuples? this ran for me.

def remove_duplicates(numlist):
nums = ()
for i in numlist:
nums = i
return nums

def remove_duplicates(dup_list):
no_dup =
for duplicate in dup_list:
if duplicate not in no_dup:
no_dup.append(duplicate)
return no_dup