Remove_duplicates - Issue with Code


#1

Hello,

I am working on remove_duplicates in Practice Makes Perfect. Here's my code:

def remove_duplicates(ls):
#Create New Empty List
new_list = []
for i in range(len(ls)):
    if ls.count(ls[i]) > 1:
        new_list.append(ls[i])
        if new_list.count(new_list[i]) > 1:
            new_list.remove(new_list[i])
return new_list

When I run this I receive the following error:

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

I'm confused as to why my list index is out of range, and which part of my code is causing the error. I've already solved this using an if val not in list > append to new list; I thought I'd try it this way but now I'm struggling.

I appreciate any assistance, thanks!


#2

Well that is one large nested function.

Go to the IDE and do this

import this

Read that, now on to your problem, depending on the tools you want to use or if you care about the order of your list.

If you don't care about order,

def remove_duplicates(lists):
    return list(set(lists))

If you do care, and want it ordered for least to greatest.

def remove_duplicates(lists):

    def _remove_duplicates(items):
        return [items.pop(item) for item in items if items.count(item) >= 1]

    return sorted(_remove_duplicates(lists))

If you want to maintain strict ordering,

def remove_duplicates(lists):

    new_list = []
    for item in lists:
        if item not in new_list:
            new_list.append(item)
    return new_list

#3

This is confusing to me: if item not in new_list:
new_list.append(item)
How do u append items to the new list without removing anything. That's like appending the input list
to the new_list. For me there is no change. the return will the input list.
Could u explain further.
Excuse my ignorance.
Thank u


#4

def remove_duplicates(one):
two = []
[two.append(item) for item in one if item not in two]

return two

#5

each item from the given list is appended to the new list if item is not in a new list already


#6

The If statement (not in) within the for loop skips the duplicate item to the next before appending into the list.
This is what I came up with:

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

print remove_duplicates([1,2,3,4,5,5,6,6,6,6,7])

#7

I did it like this:

def remove_duplicates(first_list):
second_list=[]
for item in first_list:
if item not in second_list:
second_list.append(item)
return second_list


#8

boss my code is almost same with yours but its not working here is my code
def remove_duplicates(lst):
new_lst = []
for a in lst:
if a not in lst:
new_lst.append(a)
return new_lst


#9
def remove_duplicates(lista):
new_list=[]
for i in lista:
    if i not in new_list:
        new_list.append(i)
return new_list

print remove_duplicates([1,1,2,2])


#10

def remove_duplicates(lst):
newlst = []
for i in lst:
if lst.count(i) > 1:
return False
else:
newlst.append(i)

Worked for me


#11

Hi @zeziba ,

Nice solutions :smile:

Here's another list comprehension that preserves order and does not alter the original list ...

def remove_duplicates(lst):
    return [lst[i] for i in range(len(lst)) if lst[i] not in lst[:i]]

However, it has a time complexity of O(n2), and therefore is not very efficient.


#12

Well I wrote a stupid code as below....
def remove_duplicates(example):
new = []
for i in range(len(example)):
if i < (len(example) - 1):
if example[i] not in example[i+1: len(example)]:
new.append(example[i])
else:
new.append(example[i]) #the last one should not be duplicate.
return new

print remove_duplicates([1,1,2,2])

#14