Good afternoon!
I have a list of lists in Python. Example:
mylist = [['a', 'b', 'c'],
[1, 2],
[df1, df2]]
df1, df2 are Pandas DataFrames. I want to generate result similar to itertools.product(*mylist).
The problem is that Pandas DataFrames are iterables themselves, so the result which product returns is not what I want. I want:
[('a', 1, df1),
('a', 1, df2),
('a', 2, df1),
('a', 2, df2),
('b', 1, df1),
('b', 1, df2),
('b', 2, df1),
('b', 2, df2),
('c', 1, df1),
('c', 1, df2),
('c', 2, df1),
('c', 2, df2)]
But product, of course, can not generate the desired ouptut, since it begins to iterate over df1 and df2 columns. How can I solve this problem in an elegant and Pythonic way?
Any help appreciated
>Solution :
Are you sure? product() iterates over the iterables passed to, it but only one level deep.
>>> from itertools import product
>>> mylist = [[1, 2], ['a', 'b'], [[4, 6], [8, 9]]]
>>> for x in product(*mylist):
... print(x)
(1, 'a', [4, 6])
(1, 'a', [8, 9])
(1, 'b', [4, 6])
(1, 'b', [8, 9])
(2, 'a', [4, 6])
(2, 'a', [8, 9])
(2, 'b', [4, 6])
(2, 'b', [8, 9])
See? That [4, 6] and [8, 9] are themselves iterables is irrelevant to product().