Efficient Way of making a set of tuple in which the order of tuple doesn't matters

  • A+
Category:Languages

I want to make a set of tuples in which the order of tuples shouldn't matter. For eg.- If the tuples I want to add is :

[(1,2),(1,3),(2,1)] 

It should output like this:

{(1,2),(1,3)} 

Is there any efficient way of doing this in python?

 


You can apply sorted and then tuple, followed by conversion to set:

res = set(map(tuple, map(sorted, L)))  print(res)  {(1, 2), (1, 3)} 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: