I have a list like this [(1,2), (2,1), (3,4), (5,6), (6,5)]
. How I can remove in python 3 one of sets with duplicate numbers? I want to get in output [(1,2), (3,4), (5,6)]
.
Asked
Active
Viewed 869 times
-3

Bob Napkin
- 566
- 6
- 15
-
1Does it need to be a list of sets? Do you care about the order of the sets? Also, as you have it listed it's a list of tuples. – robert Oct 29 '15 at 19:28
-
So (6,5) is the same as (5,6) then? – RobertB Oct 29 '15 at 19:29
-
@RobertB yep, the same – Bob Napkin Oct 29 '15 at 19:30
-
2if you don't care about the order: `list(map(tuple, set(map(frozenset, [(1,2), (2,1), (3,4), (5,6), (6,5)])))` See [Python eliminate duplicates of list with unhashable elements in one line](http://stackoverflow.com/q/10784390/4279) – jfs Oct 29 '15 at 19:35
-
similar question: [Python: removing duplicates from a list of lists](http://stackoverflow.com/q/2213923/4279) – jfs Oct 29 '15 at 19:41
-
@J.F.Sebastian thank you so much – Bob Napkin Oct 29 '15 at 19:48
1 Answers
3
If the order of the results doesn't matter, it is a one liner:
>>> x = [(1,2), (2,1), (3,4), (5,6), (6,5)]
>>> list(set([ tuple(set(i)) for i in x ]))
[(1, 2), (5, 6), (3, 4)]

RobertB
- 1,879
- 10
- 17