by @saigowthamr

How to remove duplicate values from a Python List

Home » python » How to remove duplicate values from a Python List

To remove the duplicate values from a python list first, we need to convert python list to a dictionary then the dictionary will automatically remove the duplicate values and convert it back to list.

Here is an example, which removes the duplicate numbers and strings from a given list.

my_list = ["a",1, 2, "b", "d", "e", 1, "a", "e"]

my_list = list(dict.fromkeys(my_list))

print(my_list)

Output:

[1, 2, 'b', 'd', 'a', 'e']

You can see our output doesn’t contain any duplicate values.

Similarly, you can also use python set() function to remove duplicate values from a list.

my_list = ["a",1,2, "b", "d", "e", 1, "a", "e"]

my_list = list(set(my_list))

print(my_list)