Python - remove duplicates from list

0 points
Created by:
Admin Dirask Community

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:

  1. create a dictionary from the list with duplicates using dict.fromkeys(my_list),
  2. convert the dictionary back to the list using list() method.


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']


['A', 'B', 'C']
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - list (popular problems)

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂