I have a set of tuples of length 2. Tuples in set could be in format (x,y)
or reversed (y,x)
.
It's guaranteed that there exist one tuple (x,y)
or (y,x)
in the set, but I can't know in advance in what order.
I need to remove either (x,y)
or (y,x)
from the set, without knowing which it is.
I tried it like this:
def flexRemove(S, tup):
try:
S.remove(tup)
except:
S.remove(tuple([tup[1], tup[0]]))
S = {(6, 1), (2, 4), (3, 8), (7, 5)}
flexRemove(S, (4, 2))
The above example removes (4,2)
or (2,4)
from the set, as desired.
Is there more elegant or more pythonic way to achieve this (without invoking the Exception)?