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

Active June 01, 2020    /    Viewed 1887    /    Comments 0    /    Edit


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


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount