5

If I have a list of tuples, where each tuple represents variables, a, b and c, how can I eliminate redundant tuples?

Redundant tuples are those where a and b are simply interchanged, but c is the same. So for this example:

tups = [(30, 40, 50), (40, 30, 50), (20, 48, 52), (48, 20, 52)]

my final list should only contain only half of the entries. One possible output:

tups = [(30, 40, 50), (20, 48, 52)]

another

tups = [(40, 30, 50), (20, 48, 52)]

etc.

Is there an easy Pythonic way to do this?

I tried using sets, but (30, 40, 50) is different from (40, 30, 50), but to me these are redundant and I'd just like to keep one of them (doesn't matter which, but if I could pick I'd prefer the low to high value order). If there was a way to sort the first 2 elements of the tuples, then using the set would work.

I am sure I could hack together a working solution (perhaps converting tuples to lists as intermediate step), but I just wanted to see if there's an easy and obvious way to do this that I'm not familiar with.

PS: This question partially motivated by PE #39. But even aside from this PE problem, I am now just curious how this could be done easily (or if).

Edit:

Just to provide a bit of context for those not familiar with PE #39 - a, b, and c represent sides of a right triangle, so I'm checking if a**2 + b**2 == c**2, clearly the order of a and b don't matter.

7
  • Actually, set((30, 40, 50)) == set((40, 30, 50)).
    – Lenna
    Jul 28, 2012 at 23:26
  • 2
    @Lenna: But, set([(30, 40, 50), (40, 30, 50)]) != set([(30, 40, 50)]). Jul 28, 2012 at 23:29
  • @JoelCornett: I was referring to the OP's statement, "I tried using sets, but (30, 40, 50) is different from (40, 30, 50)"
    – Lenna
    Jul 28, 2012 at 23:31
  • @Lenna: And that statement was correct. Your example is a set not of the tuples (30, 40, 50) and (40, 30, 50) but of the numbers 30, 40, and 50.
    – BrenBarn
    Jul 28, 2012 at 23:32
  • @Lenna Perhaps I phrased it poorly, I was trying to use the set to eliminate duplicates (as I define them as redundant) in my list of tuples (as one would ordinarily do when trying to eliminate duplicates in a list of simple values)
    – Levon
    Jul 28, 2012 at 23:33

5 Answers 5

7
set([(a,b,c) if a<b else (b,a,c) for a,b,c in tups])
8
  • Or if your Python supports it, {(a,b,c) if a<b else (b,a,c) for a,b,c in tups}.
    – Ry-
    Jul 28, 2012 at 23:33
  • @minitech Could you elaborate on your comment? Also what version of Python?
    – Levon
    Jul 28, 2012 at 23:42
  • 1
    Derivative variant: {(min(a,b), max(a,b), c) for a,b,c in tups}, or in Python 3: {(min(ab), max(ab), c) for *ab, c in tups}
    – DSM
    Jul 28, 2012 at 23:44
  • @Levon: It's "new" in Python 2.7.
    – Ry-
    Jul 28, 2012 at 23:44
  • @minitech Just to clarify, this is "set comprehension" in place of list comprehension right? Other than saving the final step of converting the list to set (which is good a good enough reason) are there are other benefits to this? (I'm happy to upvote this if you post it as an answer too)
    – Levon
    Jul 28, 2012 at 23:46
4

From your question, it seems that the first two elements of your tuples form a sub-unit within the tuple. Therefore it would seem to make sense to restructure your data as a tuple of a tuple and a third number, where the first tuple is the first two numbers in sorted order. Then you can naturally use sets:

>>> newTups = [(tuple(sorted([a, b])), c) for a, b, c in tups]
>>> newTups
[((30, 40), 50), ((30, 40), 50), ((20, 48), 52), ((20, 48), 52)]
>>> set(newTups)
set([((20, 48), 52), ((30, 40), 50)])
3
  • Is there an easy way to "unpack" these?
    – Levon
    Jul 28, 2012 at 23:40
  • @Levon: You can unpack the elements of such a tuple with, e.g. (a, b), c = newTups[0].
    – BrenBarn
    Jul 28, 2012 at 23:45
  • +1 thanks for this solution (re unpacking, I was looking for this: [(i[0][0], i[0][1],i[1]) for i in set(newTups)] and got it to work fine)
    – Levon
    Jul 29, 2012 at 0:06
4
tups = [(30, 40, 50), (40, 30, 50), (20, 48, 52), (48, 20, 52)] 
no_duplicates = list(set(tuple(sorted(tup)) for tup in tups))

Of course this is assuming that the 3rd element of each tuple will always be the largest element in each tuple, otherwise, do this:

no_duplicates = list(set(tuple(sorted(tup[:2])) + (tup[2],) for tup in tups))

As WolframH suggested, the expression tuple(sorted(tup[:2])) + (tup[2],) can be written as tuple(sorted(tup[:2])) + tup[2:], which is advantageous because it can be generalized to tuple(sorted(tup[:i])) + tup[i:], where i can be any point that one wants to separate the sorted elements from the unsorted elements.

3
  • Can't hash a list either. You'd need to convert back to tuples, I think.
    – DSM
    Jul 28, 2012 at 23:33
  • 1
    You can write (tup[2],) as tup[2:]. Jul 28, 2012 at 23:49
  • @WolframH: I like that. especially because it can be extended to sorted(tup[:i]) + tup[i:] :) Jul 29, 2012 at 0:08
2

Convert each of your tuples into a frozenset and create a set of these frozensets.

tups = [(30, 40, 50), (40, 30, 50), (20, 48, 52), (48, 20, 52)]

frozen_sets = { frozenset(x) for x in tups }

tups2 = [tuple(x) for x in frozen_sets]

This works because frozenset([1,2,3]) == frozenset([3,1,2]), in contrast to tuples, where (1,2,3) != (3,1,2).

You have to convert the tuples into frozensets rather than simple sets because you get the following error when you try to make one set a member of another set:

TypeError: unhashable type: 'set'

frozensets are hashable, and so avoid this problem.

2
  • +1 cool -- thank you, haven't worked with frozensets before. (by the way, You forgot the in in your list comprehension)
    – Levon
    Jul 28, 2012 at 23:52
  • +1. I just wrote an answer with [tuple(fs) for fs in {frozenset(t) for t in tups}] then saw it was a dup of yours an hour earlier! Deleted mine...
    – dawg
    Jul 29, 2012 at 0:42
1

If you do not care about the order for the first two elements, you don't really want to use 3-uples : just convert to a new data structure which discards the information you do not need.

result = {({x[0],x[1]},x[2]) for x in tups}
2
  • 3
    That won't quite work; you can't hash a set, so you'd have to use a frozenset.
    – DSM
    Jul 28, 2012 at 23:29
  • @DSM : oops... Too long since I last wrote any Python ! The other two answer are satisfactory, just disregard mine.
    – Jbeuh
    Jul 28, 2012 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.