Hi Folks, a question about tasks that return a tup...
# ask-community
m
Hi Folks, a question about tasks that return a tuple of items and prefect task mapping.
Please see the below code snippet
Copy code
In [52]: @task(nout=2)
    ...: def return_two_items(x):
    ...:     return x + 1, x + 2

In [53]: with Flow("test") as flow:
    ...:     left_item, right_item = return_two_items(5)

# the below is no longer correct
In [54]: with Flow("test") as flow:
    ...:     left_item, right_item = return_two_items(mapped([1, 2, 3, 4]))

# one has to do this and then unzip the reduced list of tuples to a tuple of lists
In [55]: with Flow("test") as flow:
    ...:     out = return_two_items([1, 2, 3, 4])

In [56]: flow.run().result[out].result
Out[56]: [(2, 3), (3, 4), (4, 5), (5, 6)]
Is there a way to return a tuple of two lists instead ?
Copy code
left_item = [2, 3, 4, 5]
right_item = [3, 4, 5, 6]
k
Hey @Marwan Sarieddine, short answer is no without an intermediate task to reshape because the task returns a
Tuple
so the map will generate
List[Tuple]
. while the format you want is
Tuple[List]
so the only way is to shape this with an intermediate task.
m
I see - would have been nice if one could use the same interface for both mapped and unmapped tasks
k
I know because the items have to be collected in order to reshape (think of DaskExecutor). We are aware of this, and I think there might be a feature in the works that addresses this.
👍 2