Fix Python – How to make lists contain only distinct element in Python? [duplicate]

Question

Asked By – Alex

I have a list in Python, how can I make it’s values unique?

Now we will see solution for issue: How to make lists contain only distinct element in Python? [duplicate]


Answer

The simplest is to convert to a set then back to a list:

my_list = list(set(my_list))

One disadvantage with this is that it won’t preserve the order. You may also want to consider if a set would be a better data structure to use in the first place, instead of a list.

This question is answered By – Mark Byers

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0