Python | List

Get rid of duplicated elements in a list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Normally use set() is Simple and Liner
>>> list(set(items))

# But if you want to keep the original order:

def dedupe(items):
seen = set()
for item in items: # NOTE: If the item is not hashable?
if item not in seen:
yield item
seen.add(item)

# then:
>>> a = [1, 2, 3, 3, 4, 5, 5]
>>> list(dedupe(a))
>>> [1, 2, 3, 4, 5]