Settings a previous_task variable as Jorge mentioned in my opinion is the most readable solution, in particular if you have more than one task per iteration.
Here the code:
previous_task = None
for i in range(4):
task = BashOperator(
task_id=f'task-{i}',
bash_command=f'echo {i}',
)
if previous_task is not None:
previous_task >> task
else:
previous_task = task
And here the example in case of multiple task
previous_task = None
for i in range(4):
task_x = BashOperator(
task_id=f'task_x-{i}',
bash_command=f'echo {i}',
)
task_y = BashOperator(
task_id=f'task_y-{i}',
bash_command=f'echo {i}',
)
task_x >> task_y
if previous_task is not None:
previous_task >> task_x
else:
previous_task = task_y