Let's say I have a list of tuples like:
x = [(2, 18), (18, 5), (3, 2)]
How can I sort this list of tuples based on the unique occurrence of the values in the tuples?
For example, since the number 3 only occurs in the tuple (3, 2)
and is the first value of the tuple, it should be the first entry in the list. This entry is followed by (2, 18)
because the second value (2) of (3, 2)
occurs in the first value of (2, 18)
. And finally, the last entry in the list should be (18, 5)
, since its first value matches the last value of (2, 18)
.
Expected result:
[(3, 2), (2, 18), (18, 5)]
Pls tell me if you need more info.