Home / Python Fundamentals / For Loops
Lesson 6 of 8

For Loops

Concept

A for loop repeats code for each item in a collection. The variable after for takes on each value one at a time. Your mesh uses this pattern everywhere — looping over agents, tasks, endpoints.
m3shdup/app/intrinsic.py
# Loop through idle tasks
idle_tasks = [
    {"title": "Security scan", "cooldown_hours": 24},
    {"title": "Health probe", "cooldown_hours": 2},
    {"title": "Knowledge gardening", "cooldown_hours": 4},
]

for task in idle_tasks:
    print(f"{task['title']}: every {task['cooldown_hours']}h")

# Output:
# Security scan: every 24h
# Health probe: every 2h
# Knowledge gardening: every 4h

What's happening

Each time through the loop, task becomes the next dict in the list. First iteration: task = the security scan dict. Second iteration: task = the health probe dict. And so on.

Quick Check — 3 questions

1. In for x in [1, 2, 3]: — how many times does the loop run?

2. What is x during the second iteration of for x in [10, 20, 30]:?

3. In for agent in agents: — what is agent?