user3697665
user3697665

Reputation: 307

How to reverse “reversed” duplicated tuples from a list in Python

Suppose I have a list of tuples:

a  = [(a,b), (a,b), (b,c), (b,a), (a,b), (c,b)]

How can I find all the 'reversed' duplicated tuples like (a,b) and (b,a), (b,c) and (c,b); and change elements inside tuples in a consistent order, so it can become:

a  = [(a,b), (a,b), (b,c), (a,b), (a,b), (b,c)]

Upvotes: 0

Views: 319

Answers (1)

Quentin
Quentin

Reputation: 700

Short answer

Convert tuples to list, sort the list, return it to a tuple type.

def sort_tuples(alistoftuples): return [tuple(sorted(k)) for k in alistoftuples]

Upvotes: 3

Related Questions