https://prefect.io logo
f

fabian wolfmann

10/22/2020, 9:40 PM
Hi community! which is the best way to map over a dictionary? and acces the key and value on the mapping function, i could find a example with this!
this is my example code:
Copy code
@task
def get_dict():
    new_dict = {0:"hi", 1:"yes", 2:"no"}
    return new_dict

@task
def print_nice_dict(elem):
    print(elem)

with Flow("testing") as f:
    print_nice_dict.map(get_dict)
and this is my error
Copy code
[2020-10-22 21:42:45] ERROR - prefect.FlowRunner | Unexpected error: KeyError(3)
Traceback (most recent call last):
  File "/home/fabian/anaconda3/lib/python3.7/site-packages/prefect/engine/runner.py", line 48, in inner
    new_state = method(self, state, *args, **kwargs)
  File "/home/fabian/anaconda3/lib/python3.7/site-packages/prefect/engine/flow_runner.py", line 530, in get_flow_run_state
    executor=executor,
  File "/home/fabian/anaconda3/lib/python3.7/site-packages/prefect/utilities/executors.py", line 521, in prepare_upstream_states_for_mapping
    upstream_state.result[i]
KeyError: 3
[2020-10-22 21:42:45] ERROR - prefect.testing | Unexpected error occured in FlowRunner: KeyError(3)
<Failed: "Unexpected error: KeyError(3)">
m

Mitchell Bregman

10/22/2020, 10:13 PM
Copy code
with Flow("testing") as f:
    my_dict = get_dict()
    print_nice_dict.map(my_dict.items())
f

fabian wolfmann

10/22/2020, 10:22 PM
that didnt work for me 🙃
j

Jeremiah

10/22/2020, 10:54 PM
Maybe calling
list(my_dict.items())
? Any indexable iterable should work - but for best results you may want to ensure it is stably sorted.
f

fabian wolfmann

10/22/2020, 11:04 PM
still getting error, it's seems it doesnt recognize that its a dict object
Copy code
AttributeError: 'FunctionTask' object has no attribute items.
j

Jeremiah

10/22/2020, 11:06 PM
Oh I’m so sorry I didn’t read your setup correctly. Try returning an iterable from your
get_dict()
task - instead of
new_dict
return
list(new_dict().items())
. Then you should be able to map over that directly.
I didn’t realize
my_dict
was the result of a task mI didn’t realize
f

fabian wolfmann

10/22/2020, 11:09 PM
great it did work!
j

Jeremiah

10/22/2020, 11:09 PM
👌 awesome, sorry for the confusion!
f

fabian wolfmann

10/22/2020, 11:14 PM
no problem! just to know, why a
list
is more stably sorted than a
dict
?
also try with OrderedDict from collections and didnt worked
j

Jeremiah

10/23/2020, 12:45 AM
dict
is sorted by convention in Python 3.6+, but you can’t, for example, get the 2nd element by accessing it with
[2]
, it can only be accessed by key.
2 Views