14/15 remove_duplicates - THere is any easy way?


#1

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])



#2

remove the prints :wink:


#3

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


#4

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


#5

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

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

Thanks!


#6

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


#7

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

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


#9

Shorter code bro:


#10

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

#11

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])


#12

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


#13

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

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

#14

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


#15

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


#16

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


#17

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


#18

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)}


#19

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:


#20

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

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


#21

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