Can't make remove_duplicates work!


#1



Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [] instead of [4, 5]


I thought the list.append(i) would work and it hasnt


def remove_duplicates(items):
    items = []
    new = []
    for i in items:
        for c in range(0,len(items)):
            print i[c]
            if c==0:
                new.append(i)
            elif i[c] != i[c-1]:
                new.append(i)
    return new


#2

here:

items = []

you overwrite the function parameter, why?

and why do you have two loops?


#3

I put in the two loops to define c and i, i dont understand why i cant use c to call the index of the list


#4

you overwrite items with an empty list? did you remove that? If so, can you post an updated version of your code


#5

def remove_duplicates(items):
    new = []
    for i in items:
        for c in range(0,len(items)):
            print i[c]
            if c==0:
                new.append(i)
            elif i[c] != i[c-1]:
                new.append(i)
    return new

error now is
Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: 'int' object has no attribute 'getitem'


#6

double loops are tricky, a problem arises here:

print i[c]

i is a integer. how can you access a integer by index? You can do this for lists, yes. But not for integers

if you add a function call:

def remove_duplicates(items):
    new = []
    for i in items:
        for c in range(0,len(items)):
            print i[c]
            if c==0:
                new.append(i)
            elif i[c] != i[c-1]:
                new.append(i)
    return new
print remove_duplicates([4, 5, 5, 4])

you can actually see where you go wrong


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.