14/15


#1

I don't understand why doesn't this work???
def remove_duplicates(x):
new = []
for n in x:
if n not in new:
return new.append(n)
return new


#2

you can only call return once in a function and after that the function will terminate(we are done with the function). Any code after that is UNREACHABLE and will not be executed.
Now in your code you will keep getting None being thrown at you because

new.append(n)

is an operation that modifies the a list in memory, it does not return(give) any value back to you(in a sense is a void operation(like in Java) ) and hence gives you None.

To fix this you need to remove the first return word
Hope this helped?


#3

When you add numbers in set, it contains them without copies. I think it is the best way to complete these tasks
def remove_duplicates(lst):
seta = set ()
for i in range(0, len(lst)):
a.add(lst[i])
return a
print (remove_duplicates([1, 1, 2, 2]))


#4

Are you sure the code you have provided is the best way(efficient)? can you justify why it is?


#5

So are you saying when I do new.append(n), it will repeat it self over again and it will do it once?
Well that's useful... but how am I supposed to fix it?


#6

JUST THE WORD nothing else


#7

because adding items in set takes less resources than: check if your item is already in list and if it false, add it. its 1 operation instead of 2


#8

What he is saying is yes your way solves the problem but for the most part is not the MOST efficient.
for example click the edit code link to edit and see comments:
http://goo.gl/RTykZR

your approach with the sample takes 17 steps
with the second it takes 32

comment out your approach and try the second in the same manner in either case it takes 6 steps


#9

That's a very interesting point.
But were 'sets' explained in the course of Codeacademy so far? I didn't recall them so probably that's why I went, like the example above, with the most obvious 'list.append' solution.


#10

They didn't. Sorry:smirk:


#11

You would have to scroll down on the same documentation page as .pop .remove .del to see it.


#12


Instead of using two returns, as you did, I tried to use the set([]) function, per the advice below. This code returns the correct answer, but, for reasons unknown to me, creates an endless loop. I attempted to use an else: break, but it still resulted in an endless loop. If anyone knows why this occurs and what can be added to prevent it, please let me know?


#13

You need to re-evaluate your code it does not return a list with no duplicates. because the found variable is just an empty set hence this line of code
if item not in found: will always evaluate to true and will add(append) every item in dooops to keep