https://prefect.io logo
#prefect-community
Title
# prefect-community
a

ale

05/23/2022, 1:23 PM
Hey folks 👋 I tried mapping over the entries of a
dict
, but got the following error:
Copy code
At least one upstream state has an unmappable result.
Looking at the docs, it seems that
map
should work with an
Iterable
. Afaik,
dict
is an
Iterable
in Python, so I’m a bit confused 😅 I’m using Prefect
0.15.16
v

Vadym Dytyniak

05/23/2022, 1:26 PM
What other args you pass in the task?
a

ale

05/23/2022, 1:27 PM
I am passing two arguments: • a dict (the object on which the mapping should be applied) • another
unmapped
object
a

Anna Geller

05/23/2022, 1:44 PM
Mapping works with lists - you could map over a list of dictionaries, but not over dictionaries
e

emre

05/23/2022, 1:44 PM
I don't think dict is an iterable, but
dict.items()
is
☝️ 1
a

ale

05/23/2022, 1:46 PM
Thanks for the suggestions folks, much appreciated as usual! 🙌 🙏 gratitude thank you
👍 1
a

Anna Geller

05/23/2022, 1:47 PM
I think the easiest would be to map over:
Copy code
list(your_dict.values())
one task could return the above based on dict as input and pass it to the task doing mapping - just one possibility among many
@ale I can confirm it must be a list actually, returning just sample_dict.values() would fail but wrapping it with list() works:
Copy code
import prefect
from prefect import task, Flow


@task
def get_iterable():
    sample_dict = dict(a=1, b=2, c=3)
    return list(sample_dict.values())
    # return sample_dict.values()


@task
def log_output(x):
    <http://prefect.context.logger.info|prefect.context.logger.info>(x)


with Flow("map_dict_test") as flow:
    iterable_input_from_dict = get_iterable()
    log_output.map(iterable_input_from_dict)

if __name__ == "__main__":
    flow.run()
👍 1
7 Views