Getting unique tuples from a list

I have a list of tuples whose elements are:

aa = [('a', 'b'), ('c', 'd'), ('b', 'a')] 

I want to be considered ('a', 'b') and ('b', 'a')as the same group and I want to retrieve only unique tuples. So the output should look like this:

[('a', 'b'), ('c', 'd')]

How can I achieve this effectively since my list consists of millions of such tuples?

+4
source share
2 answers

Convert to frozenset, hash and get:

In [193]: map(tuple, set(map(frozenset, aa))) # python2
Out[193]: [('d', 'c'), ('a', 'b')]

Here is a slightly more readable version with a list:

In [194]: [tuple(x) for x in set(map(frozenset, aa))]
Out[194]: [('d', 'c'), ('a', 'b')]

Note that for your particular use case, the tuple list is not the best choice of data structure. Consider storing your data as a set for starters?

In [477]: set(map(frozenset, aa))
Out[477]: {frozenset({'a', 'b'}), frozenset({'c', 'd'})}
+7

set frozenset, , list tuple, :

>>> {frozenset(x) for x in aa}
{frozenset({'c', 'd'}), frozenset({'a', 'b'})}
0

Source: https://habr.com/ru/post/1683956/


All Articles