Skip to content Skip to sidebar Skip to footer

Remove Duplicates From List Python

I'm trying to write a program that removes duplicates from a list, but my program keeps throwing the error 'list index out of range' on line 5, if n/(sequence[k]) == 1:. I can't fi

Solution 1:

I strongly suggest Akavall's answer:

list(set(your_list))

As to why you get out of range errors: Python passes by reference, that is sequence and new_list still point at the same memory location. Changing new_list also changes sequence.

And finally, you are comparing items with themselves, and then remove them. So basically even if you used a copy of sequence, like:

new_list = list(sequence)

or

new_list = sequence[:]

It would return an empty list.

Solution 2:

Your error is concurrent modification of the list:

for k in range(len(sequence)):
    if n/(sequence[k]) == 1:
        new_list.remove(sequence[k])

It may seem removing from new_list shouldn't effect sequence, but you did new_list = sequence at the beginning of the function. This means new_list actually literally is sequence, perhaps what you meant is new_list=list(sequence), to copy the list?

If you accept that they are the same list, the error is obvious. When you remove items, the length, and the indexes change.

P.S. As mentioned in a comment by @Akavall, all you need is:

sequence=list(set(sequence))

To make sequence contain no dupes. Another option, if you need to preserve ordering, is:

from collections importOrderedDict
sequence=list(OrderedDict.fromkeys(sequence))

Solution 3:

If you don't like list(set(your_list)) because it's not guaranteed to preserved order, you could grab the OrderedSet recipe and then do:

from ordered_set import OrderedSet

foo = list("face a dead cabbage")
print foo
printlist(set(foo)) # Order might changeprintlist(OrderedSet(foo)) # Order preserved

Solution 4:

# like @Akavall suggesteddefremove_duplicates(sequence):
    # returns unsorted unique listreturnlist(set(sequence))

# create a list, if ele from input not in that list, append.defremove_duplicates(sequence):
    lst = []
    for i in sequence:
        if i notin lst:
            lst.append(i)
    # returns unsorted unique listreturn lst

Post a Comment for "Remove Duplicates From List Python"