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:
print no_dup
return no_dup

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


remove the prints :wink:


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.


Didn't know about "set" element. Good to know!

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



@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!


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

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


Shorter code bro:


How about this

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


def remove_duplicates(numbers):
new_list =[]
for x in numbers:
if x not in new_list:

return new_list

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


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


Wouldn't the shortest possible code be a lambda?

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


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


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


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:
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:
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:
return no_dup