How to Remove Duplicates From a List in Python


In this example we will show how to remove duplicates from a list in Python.

Source Code

my_list = ['A', 'B', 'C', 'A', 'B', 'C']
my_list_new = list(dict.fromkeys(my_list))
print(my_list_new)

Output:

['A', 'B', 'C']
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Remove Duplicates From a List in Python


In this example we will show how to use functions to remove duplicates from a list in Python.

Source Code

def my_function(x):
    return list(dict.fromkeys(x))

my_list = ['A', 'B', 'C', 'A', 'B', 'C']
my_list_new = my_function(my_list)
print(my_list_new)

Output:

['A', 'B', 'C']
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments