Concepts 🚀 Project 🧮 Labs
📚 Learn / Python / Loops — for, while, comprehensions

Loops — for, while, comprehensions

Iterate over any iterable. List comprehensions are the Pythonic way to build lists.

Code example

# for loop
for i in range(3):
    print(i)

# while loop
n = 10
while n > 1:
    n //= 2

# comprehension (preferred)
squares = [x*x for x in range(10) if x % 2 == 0]

How it works

`for` iterates over any iterable (lists, tuples, dicts, strings, files, generators). `while` runs until a condition is false. The Pythonic idiom for building a list is a comprehension: `[expr for x in iter if cond]` — it's faster than a for+append loop because the list is built in C. OpenBenchML uses comprehensions everywhere — e.g. leaderboard serialisation: `[{**r.__dict__} for r in rows]`.

🔍 Where this is used in OpenBenchML Every route that returns a list of models/datasets/competitions uses a comprehension to build the response.