EN
Python - remove duplicates from list
0
points
In this article, we would like to show you how to remove duplicates from list in Python.
Quick solution:
my_list = ['A', 'A', 'B', 'C']
my_list = list(dict.fromkeys(my_list))
print(my_list) # ['A', 'B', 'C']
Practical example
There are three steps that we take to remove duplicates from a list:
- create a dictionary from the list with duplicates using
dict.fromkeys(my_list)
, - convert the dictionary back to the list using
list()
method.
Note:
Creating dictionary with the list items as keys will automatically remove duplicates because dictionaries cannot have duplicate keys.
Practical example:
my_list = ['A', 'A', 'B', 'C']
my_list = list(dict.fromkeys(my_list))
print(my_list) # ['A', 'B', 'C']
Output:
['A', 'B', 'C']