Remove duplicate tuples regardless of order with the same elements in Python 3.5 generator

I have a tuple generator and I need to delete tuples containing the same elements. I need this output for iteration.

Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3))

Output = ((1, 1), (1, 2), (1, 3)) Output order doesn't matter

I checked this question, but it is about lists: Remove duplicate tuples with same elements in nested Python list

I use generators for the fastest results since the data is very large.

EDIT: In addition to the accepted answer, I also found that I can use the itertools combination method.

unique_combinations = itertools.combinations(Input, 2)

      

-1


source to share


1 answer


You can normalize the data by sorting it and then add to the set to remove duplicates



>>> Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3))
>>> Output = set(tuple(sorted(t)) for t in Input)
>>> Output
{(1, 2), (1, 3), (2, 3), (1, 1), (3, 3)}

      

+2


source







All Articles