Remove duplicates


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#


Oops, try again. remove_duplicates([]) resulted in an error: list index out of range


Code works fine if i call it myself as did in the code below,but still it shows the above error


def remove_duplicates(l):
    n=[]
    n.append(l[0])
    
    for i in range(len(l)):
        p=0
        for k in range(len(n)):
            if l[i]==n[k]:
                p+=1
        if p<1:
            n.append(l[i])
    return n
print remove_duplicates([4,4,5,6,8,8,7,1,1])


#2

try this:
def remove_duplicates(nums):
return list(set(nums))


#3

or u can use this:

def remove_duplicates(nums):
new = []
for i in nums:
if i not in new:
new.append(i)
return new


#4

This works:

def remove_duplicates(x):
z=[]
c=0
e=1
for a in x:
for b in range(e,len(x)):
if a==x[b]:
c=1
if c!=1:
z.append(a)
e+=1
c=0
return z


#5

code works fine,but i wanna know why the error


#6

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