3

Possible Duplicate:
How do you remove duplicates from a list in Python whilst preserving order?
Does Python have an ordered set?

I find myself doing the following quite frequently:

list_of_items # I have a list of items
set(list_of_items) # I want to remove duplicates

However, in converting to a set, I lose the initial ordering I had in the list, which is important.

What is the best way to remove duplicates in a list AND keep the initial (relative) ordering of the list?

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
  • Definitely a duplicate. One of the answers is also a near duplicate of the answer from the duplicate question @Levon mentioned. – istruble Jun 15 '12 at 23:32

3 Answers3

7

This is somewhat of an abuse of list comprehensions, but:

seen = set()
item_list = [seen.add(item) or item for item in item_list if item not in seen]
kindall
  • 178,883
  • 35
  • 278
  • 309
0

Use a dictionary to remove duplicates, use the data as keys and anything as values. If it's in the dict, don't add it again.

d = dict()
for i in range(0, len(list_of_items)):
    if(list_of_items[i] not in d):
        d[list_of_items[i]] = i
        list_of_items2.append(list_of_items[i])
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0

Slightly hackish, but ...

item_list = [x[1] for x in sorted(x[::-1] for x in dict(zip(item_list[::-1],range(len(item_list)))).items())][::-1]
gvl
  • 903
  • 7
  • 16