Hey folks :wave: I tried mapping over the entries ...
# prefect-community
a
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
What other args you pass in the task?
a
I am passing two arguments: • a dict (the object on which the mapping should be applied) • another
unmapped
object
a
Mapping works with lists - you could map over a list of dictionaries, but not over dictionaries
e
I don't think dict is an iterable, but
dict.items()
is
☝️ 1
a
Thanks for the suggestions folks, much appreciated as usual! 🙌 🙏 gratitude thank you
👍 1
a
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