How to remove (filter) the duplicates in a python list ?

How to remove (filter) the duplicates in a python list ?

Daidalos June 01, 2020


Examples of how remove (filter) the duplicates in a python list:

1 -- Create a list

Let's consider the following list:

>>> l = ['a','a','b','c','d','d','d']

the goal here is to remove the duplicates in the list.

2 -- Remove the duplicates using a for loop

A first solution is to use a for loop, example:

>>> lwd = []
>>> for i in l:
...     if i not in lwd: lwd.append(i)
... 
>>> lwd
['a', 'b', 'c', 'd']

2 -- Remove the duplicates using a dictionary

Another solution is to use a dictionary (since dictionary keys are unique), example:

>>> lwd = list(dict.fromkeys(l))
>>> lwd
['a', 'b', 'c', 'd']

4 -- References