Making a sequence of tuples unique by a specific element

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:

def unique_by_key(elements, key=None):
    if key is None:
        # 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:

from operator 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.

Leave a Comment