Alex Alex - 6 months ago 14
Python Question

How to make lists distinct?

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

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.

Comments