14:15 remove_duplicate exercise. Can't point out the mistake in the code. Please Help!


#1

Can someone please help me with this exercise by pointing out my mistakes as i have been stuck for a while now.


def remove_duplicates(given):
new_list=[]
given=given.sort()

for i in range(0,len(given)-1):
    if given[i]!=given[i+1]:
        new_list.append(given[i])    

if given[len(given)-1]!=given[len(given)-2]:
    new_list.append(given[len(given)-1])

return new_list

Thanks in advance.


#2

Test your function..

remove_duplicates([1, 1, 2])

Outcome:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "delme.py", line 5, in remove_duplicates
    for i in range(0,len(given)-1):
TypeError: object of type 'NoneType' has no len()

Error says you are trying to get the length of None on line 5, so what are you giving len as an argument there? given.

So why is given's value None? Look at what you did to given, what did you last assign to it?

list.sort sorts the list it is called on, it doesn't return anything (None)


#3

You don't necessary need to sort the list. You are on the right track by creating a new empty list though. You also don't need to measure the length of the incoming list at all for this one. I think that was a trap for us :confused:

Let's call the old list: "OLD"
and the new list: "NEW"

Try appending each value from the OLD into the new one at a time using the for loop.

for x in OLD:

The next part is tricky but rather novel from what we think of. We check to see if the number we appended from OLD is already in NEW. I found it easier to use "not in" to determine if it should be appended or not.

if x not in NEW:

No else statements are needed here. You just appended the value from OLD if it wasn't in NEW to begin with.

NEW.append(x)

You then just have to return NEW and that should run well God willing.

In the end it should look like something like this:

def remove_duplicates(OLD):
    NEW = []
    for x in OLD:
        if x not in NEW:
            NEW.append(x)
    return NEW

You can even add strings to the list if you wanted to try it out.

Hope that helps~


#4

Hi i think that it is the easy way to do the exerecise:
def eliminar_repetidos(lista):
lista1=[]
for item in lista:
if item not in lista1:
lista1.append(item)
return lista1


#5

It's probably not very hard to find, you just need to dig into it.

Add print statements so that you can compare what happens to what you expect.

If you don't know what to expect well then you'll have to figure that out first, no point in writing code if you don't know what you want to happen.

Quite a lot happens in several of your lines, you can split them up a bit into several lines, save stuff in variables, so that it gets easier to add prints to check what is going on.

I find it helpful to not just print out the values, but to also write out explanations of what the values are, so that your program prints out what it is doing in plain English and so that even somebody completely unaware of what a computer is can point out where the it goes wrong.