Remove Duplicates from a Python List

Here are 2 ways to remove duplicates from a Python list:

1. Using set (order is not preserved):

my_list = ["aa", "aa", "dd", "xyz", "aa", "pp", "pp"]

unique_list = list(set(my_list))

print(unique_list)

The result:

['pp', 'aa', 'dd', 'xyz']

2. Using dict.fromkeys() where the order is preserved:

my_list = ["aa", "aa", "dd", "xyz", "aa", "pp", "pp"]

unique_list = list(dict.fromkeys(my_list))

print(unique_list)

The result:

['aa', 'dd', 'xyz', 'pp']

Create a function to remove duplicates

Here is a function to remove all duplicates from a Python list:

def remove_duplicates(my_list: list) -> list:
"""
Function to remove all duplicates in a Python list.

:param my_list: The input list that may contain the duplicates to be removed.
:return: A unique list without the duplicates.
"
""
return list(dict.fromkeys(my_list))


example_list = ["aa", "aa", "dd", "xyz", "aa", "pp", "pp"]

print(remove_duplicates(example_list))

The result:

['aa', 'dd', 'xyz', 'pp']

Leave a Comment