Making a sequence of tuples unique to 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 tuples from 'a' that have a common second element except for one (any of them).

In the above example, I would like to get a new output a = ((1,2),(3,4))

In other words, I would like to exclude tuples that are considered duplicate elements in the second position of the tuple.

I would like to know the most efficient way to achieve this and also know if I can do the same with lists instead of tuples?

+3


source to share


1 answer


You can create a dictionary from your elements so that you would like to be unique as a key, and then extract the values. This works for anything where the "unique" subelement is hashable. Integers are hashed:

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 feature is pretty general; it can be used to retrieve "unique" items based on any attribute, as long as any returned calls key

can be used as a key in the dictionary. The order will not be saved and the last item for the key is currently winning.

With the above function, you can use operator.itemgetter()

object
or lambda to extract the second value from each element. Then this 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 this back by calling tuple()

the result.

+6


source







All Articles