Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing permutations from a list of tuples [duplicate]

Any help with this question is appreciated.

I have a list of tuples

a = [(1,2), (2,1), (1,3), (1,4), (4,1)]

and I need to remove duplicates of a certain type: (1,2) and (2,1) are considered duplicates according to my definition. Required output

a = [(1,2), (1,3), (1,4)]

Thanks in advance

like image 437
user62089 Avatar asked Mar 12 '13 03:03

user62089


1 Answers

You could sort them and then remove duplicates with set():

>>> set(tuple(sorted(l)) for l in a)
    set([(1, 2), (1, 3), (1, 4)])
like image 178
Blender Avatar answered Sep 19 '22 02:09

Blender