Python way of removing reversed duplicates in list

Published on Author Code Father
Python way of removing reversed duplicates in list

If you need to preserve the order of the elements in the list then, you can use a the sortedfunction and set comprehension with map like this:

lst = [0, 1], [0, 4], [1, 0], [1, 4], [4, 0], [4, 1]
data = {tuple(item) for item in map(sorted, lst)}
# {(0, 1), (0, 4), (1, 4)}

or simply without map like this:

data = {tuple(sorted(item)) for item in lst}

Another way is to use a frozenset as shown here however note that this only work if you have distinct elements in your list. Because like set, frozenset always contains unique values. So you will end up with unique value in your sublist(lose data) which may not be what you want.

To output a list, you can always use list(map(list, result)) where result is a set of tuple only in Python-3.0 or newer.

 

Comments

comments