https://prefect.io logo
e

Eli Treuherz

01/26/2022, 5:20 PM
Say I’ve got two or more lists I’d like to map a task over. If I provide two mappable arguments to
Task.map()
it iterates through both of them together like
zip()
. Is there a way to loop over every combination instead, like
itertools.product()
?
k

Kevin Kho

01/26/2022, 5:20 PM
You need a task to make that cross-product
e

Eli Treuherz

01/26/2022, 5:21 PM
so an un-mapped task taking both lists, that returns something like a list of tuples? And then I can map my other task over that?
I’ll have to figure out the destructuring but should be doable. Was hoping there was an easier way. Thanks for the quick reply!
k

Kevin Kho

01/26/2022, 5:22 PM
Yes exactly. Try returning a tuple of lists and then use this syntax
Copy code
a, b = product()
other_task.map(a, b)
e

Eli Treuherz

01/26/2022, 5:23 PM
doesn’t that just put me back where I started?
tuple of lists vs list of tuples
k

Kevin Kho

01/26/2022, 5:25 PM
I mean the
a
and
b
here will be expanded lists already like
a=[1,1,1,2,2,2,3,3,3]
,
b=["a","b",""c","a","b",""c","a","b",""c"]
. I dunno I might just be confusing you 😅. I think you understand it already
e

Eli Treuherz

01/26/2022, 5:25 PM
Ah, gotcha. Yes I guess that’d do it!
Copy code
In [14]: tuple(map(list, zip(*product(["A", "B", "C"], ["1", "2", "3"]))))
Out[14]:
(['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
 ['1', '2', '3', '1', '2', '3', '1', '2', '3'])
💪
k

Kevin Kho

01/26/2022, 5:37 PM
Yeah exactly!
5 Views