14/15 I dont know what is wrong


#1

Can someone advise what did I do wrong?

def remove_duplicates(number):
newnumber = []
newnumber.append(number[0])
for i in range (1, len(number)):
if newnumber[i - 1] != number[i]:
newnumber.append(number[i])
return newnumber


#2

Hi @designrunner60107,

Here's a little trick:

Whenever you would like to paste code, since this forum supports Markdown you can use three backticks before and after your code block like so:

```
def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
else:
print "Odelay!"
return sum
```

and it will retain proper indentation and even have nifty syntax highlighting:

def my_func(x):
    my_var = 0
    if x > my_var:
      sum = my_var + x
    else:
      print "Odelay!"
    return sum

That being said, looking at your code and indenting it like so:

def remove_duplicates(number):
    newnumber = []
    newnumber.append(number[0])
    for i in range (1, len(number)):
        if newnumber[i - 1] != number[i]:
            newnumber.append(number[i])
    return newnumber

I run into a list index out of range error message. The reason likes in the first line of your for loop. Your if condition is invalid.

Let's try and cleanup your code a little bit first:

def remove_duplicates(number):
    newnumber = []

#    You will not need the following line if we simplify your for loop a little...
#    newnumber.append(number[0])

#    So instead of:
#    for i in range (1, len(number)):

#   The for loop will iterate through all items of number if invoked like this:
    for i in number:

#       This is your invalid if condition. inside a loop, i and number[i] won't
#       mean the same thing... A simple debug print statement will show this to you
#        if newnumber[i - 1] != number[i]:
#            newnumber.append(number[i])

#        print "i is: " + str(i)
#        print "number[i] is: " + str(number[i])

#       What you want is to append i to your newnumber...
        if i not in newnumber:
            newnumber.append(i)

    return newnumber

# so using the following list of numbers    
dupes = [1,2,3,12,2,3,1,2,2,3,1,2,3,33,1,2,3,4,5,6,6,78,9,0]

print remove_duplicates(dupes)
# will return [1, 2, 3, 12, 33, 4, 5, 6, 78, 9, 0]

Hope this helped you understand a little better. If not, try to go back to previous exercises about loops and if statements or you can also read some extra documentation.

Good luck! :smile:

`


#3

This is a simple example:

def remove_duplicates(item):
for i in range(len(item)):
if i == len(item):
delete.item[i]
return i


#4

def remove_duplicates(seq):
new=['']
for i in seq:
for k in new:
if i not in new:
new.append(i)
new.remove('')
return new
m=[1,2,3,1,2,4,2,3,0,0,4]
print remove_duplicates(m)


#5

def remove_duplicates(numbers):
return set(numbers)
print remove_duplicates([1, 1, 2, 4])


#6

def remove_duplicates(num):

    if num == []:
        return 0
    out = []
    out.append(num[0])
    
    
    for n in num:
        for o in out:
            if n == o:
                break
        else:
            out.append(n)
    
    return out

#7

This appends the number (x), from the list (a), to your new list (nodup) - only if it is not in your new list (nodup) already.

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

a = [2,2,3,3,4,4]
print remove_duplicates(a)


#8

This appends the number (x), from the list (a), to your new list (nodup) - only if it is not in your new list (nodup) already.