How to make lists contain only distinct element in Python?

How to make lists contain only distinct element in Python?

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 wont 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.

Modified versions of http://www.peterbe.com/plog/uniqifiers-benchmark

To preserve the order:

def f(seq): # Order preserving
   Modified version of Dave Kirby solution 
  seen = set()
  return [x for x in seq if x not in seen and not seen.add(x)]

OK, now how does it work, because its a little bit tricky here if x not in seen and not seen.add(x):

In [1]: 0 not in [1,2,3] and not print(add)
add
Out[1]: True

Why does it return True? print (and set.add) returns nothing:

In [3]: type(seen.add(10))
Out[3]: <type NoneType>

and not None == True, but:

In [2]: 1 not in [1,2,3] and not print(add)
Out[2]: False

Why does it print add in [1] but not in [2]? See False and print(add), and doesnt check the second argument, because it already knows the answer, and returns true only if both arguments are True.

More generic version, more readable, generator based, adds the ability to transform values with a function:

def f(seq, idfun=None): # Order preserving
  return list(_f(seq, idfun))

def _f(seq, idfun=None):  
   Originally proposed by Andrew Dalke 
  seen = set()
  if idfun is None:
    for x in seq:
      if x not in seen:
        seen.add(x)
        yield x
  else:
    for x in seq:
      x = idfun(x)
      if x not in seen:
        seen.add(x)
        yield x

Without order (its faster):

def f(seq): # Not order preserving
  return list(set(seq))

How to make lists contain only distinct element in Python?

one-liner and preserve order

list(OrderedDict.fromkeys([2,1,1,3]))

although youll need

from collections import OrderedDict

Leave a Reply

Your email address will not be published. Required fields are marked *