What is a Pythonic way to make combined tuple from a list?
e.g. From:
a = [['A','B','C'], ['D', 'E', 'F']]
To:
[('A','D'), ('A', 'E'), ..., ('C', 'E'), ('C','F')]
What is a Pythonic way to make combined tuple from a list?
e.g. From:
a = [['A','B','C'], ['D', 'E', 'F']]
To:
[('A','D'), ('A', 'E'), ..., ('C', 'E'), ('C','F')]
Use itertools.product
. If you have a list of lists, use the "splat" operator, *
, to unpack them. product
takes an arbitrary number of iterables:
>>> a = [['A', 'B'], ['C', 'D'], ['E', 'F']]
>>> list(itertools.product(*a))
[('A', 'C', 'E'),
('A', 'C', 'F'),
('A', 'D', 'E'),
('A', 'D', 'F'),
('B', 'C', 'E'),
('B', 'C', 'F'),
('B', 'D', 'E'),
('B', 'D', 'F')]