Skip to content Skip to sidebar Skip to footer

Making A Sequence Of Tuples Unique By A Specific Element

So I have a tuple of tuples a = ((1, 2), (7, 2), (5, 2), (3, 4), (8, 4)) I would like to remove all the tuples from 'a' which have the a common second element, except for one (any

Solution 1:

You could create a dictionary from your elements, with whatever you wanted to be unique as the key, then extracting the values. This works for anything where the 'unique' sub-element is hashable. Integers are hashable:

defunique_by_key(elements, key=None):
    if key isNone:
        # no key: the whole element must be unique
        key = lambda e: e
    return {key(el): el for el in elements}.values()

This function is pretty generic; it can be used to extract 'unique' elements by any trait, as long as whatever the key callable returns can be used as a key in a dictionary. Order will not be preserved, and currently the last element per key wins.

With the above function you can use a operator.itemgetter() object or a lambda to extract your second value from each element. This then works for both a sequence of tuples and a sequence of lists:

fromoperator import itemgetter

unique_by_second_element = unique_by_key(a, key=itemgetter(1))

Demo:

>>>from operator import itemgetter>>>a = ((1, 2), (7, 2), (5, 2), (3, 4), (8, 4))>>>unique_by_key(a, key=itemgetter(1))
[(5, 2), (8, 4)]
>>>b = [[1, 2], [7, 2], [5, 2], [3, 4], [8, 4]]>>>unique_by_key(b, key=itemgetter(1))
[[5, 2], [8, 4]]

Note that the function always returns a list; you can always convert that back by calling tuple() on the result.

Post a Comment for "Making A Sequence Of Tuples Unique By A Specific Element"