2

In a list, I have duplicate elements that I want to remove.

The following code doesn't work:

Note: temp containts the list of indexes of elements that I want to remove.

x is my list.

temp = self.list_duplicates(x)
for index in tmp:
   del x[index] 
Stack Over
  • 425
  • 2
  • 8
  • 18
  • 4
    It's worth noting that if you just want to keep unique items in a list, you can use a [`set`](https://docs.python.org/3/tutorial/datastructures.html#sets). – Aurora0001 Jul 28 '16 at 16:18
  • 1
    This is true, but sets don't have order, which can make them not work for some problems. – Oscar Smith Jul 28 '16 at 16:21
  • Also see [Removing items from a list while iterating over the list](http://sopython.com/canon/95/removing-items-from-a-list-while-iterating-over-the-list) – PM 2Ring Jul 28 '16 at 16:24

4 Answers4

2

Build a new list with a comprehension:

x = [element for (i,element) in enumerate(x) if i not in temp]

If you want to remove only duplicates, i.e. leaving one copy of the original, there is a better way to do that:

from collections import OrderedDict
x = list(OrderedDict.fromkeys(x))
wim
  • 338,267
  • 99
  • 616
  • 750
1

x.pop(index) will remove the item at index. However, x = [x[i] for i in range(len(x)) if x[i] not in x[:i]] will remove the duplicates more quickly.

wim
  • 338,267
  • 99
  • 616
  • 750
Oscar Smith
  • 5,766
  • 1
  • 20
  • 34
  • `for i in len(x)` will raise `TypeError`. And `x.pop(index)` will offset the other indices, so careful with that. – wim Jul 28 '16 at 16:21
  • Good catch on the first, fixed. The second is true, but that will be true any time you remove an element from the middle of a list. – Oscar Smith Jul 28 '16 at 16:23
0

You can filter the list with the following:

Edit: Works now for list of indices

x = list(filter(lambda item: x.index(item) not in temp, x))
Rafael Albert
  • 445
  • 2
  • 8
0

No one addressed the first part of the question. So, for removing duplicate items its wise to use set(), it removes the duplicate items and returns a arbitrary list of unique items.

lst = [1,1,2,3,4,4]
new_lst = list(set(lst))
print(lst)

It will return unique list of elements in an arbitrary manner Eg : [1,4,2,3]

atsh3
  • 3
  • 3