Let's say I have the following elements: ['A', 'B', 1, 2]
My idea is to get the following combinations:
('A', 1)
('A', 2)
('B', 1)
('B', 2)
But these are not all the combinations of the above sequence, e.g. I'm not considering (in purpose) ('A', 'B')
or (1, 2)
Using itertools.combinations
, of course, gets me all the combinations:
from itertools import combinations
combinations(['A', 'B', 1, 2], 2)
# [('A', 'B'), ('A', 1), ('A', 2), ('B', 1), ('B', 2), (1, 2)]
It's possible for me to internally group the elements that cannot go together:
elems = [('A', 'B'), (1, 2)]
However, combinations
does not expect iterables inside other iterables, so the outcome is not really unexpected: [(('A', 'B'), (1, 2))]
. Not what I want, nonetheless.
What's the best way to achieve this?