Languages
[Edit]
EN

Python - remove duplicates from list

0 points
Created by:
Admin Dirask Community
1840

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.

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

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.

â€ïžđŸ’» 🙂

Join