<@ULVA73B9P> what's the best to use : .map or for ...
# ask-marvin
f
@Marvin what's the best to use : .map or for loop?
m
thought for 57.9 seconds
Great question — quick clarification: do you mean Prefect’s old
.map
from Prefect 1.x, or Python’s built-in
map()
vs a
for
loop? If you’re asking in the context of Prefect 2 (which you’re using): - Prefer a
for
loop or list comprehension with
task.submit(...)
- Prefect 2 does not support `.map`; that was a Prefect 1.x feature - The
submit
pattern is clearer, fully supported, and integrates with retries, caching, and concurrency Example (fan-out/fan-in):
Copy code
from prefect import flow, task
from prefect.task_runners import ConcurrentTaskRunner

@task
def transform(x):
    return x * 2

@flow(task_runner=ConcurrentTaskRunner())
def my_flow(items):
    futures = [transform.submit(x) for x in items]  # fan-out
    results = [f.result() for f in futures]         # fan-in
    return sum(results)

my_flow(range(10))
Notes: - Using Python’s
map(transform.submit, items)
also works but is less readable; the `for`/comprehension approach is preferred. - Concurrency is controlled by your task runner (e.g.,
ConcurrentTaskRunner
,
DaskTaskRunner
). The loop vs
map
doesn’t change performance by itself. If you meant Prefect 1.x’s
.map
, the idiomatic Prefect 2 equivalent is exactly the
submit
loop above. Would you like a side-by-side example for migrating from Prefect 1 mapping to Prefect 2?