for loop
Iterates over any iterable: list, str, range, dict, set or generator. Use range for index loops, enumerate when you need index and value, zip to walk sequences in parallel, and break/else for early exit.
Syntax
for x in nums:
print(x)
Index loop with range
for i in range(n): # 0 .. n-1
...
for i in range(1, n): # 1 .. n-1
...
for i in range(0, n, 2): # step 2
...
for i in range(n - 1, -1, -1): # n-1 down to 0
...
Index and value: enumerate
for i, x in enumerate(nums):
...
for i, x in enumerate(nums, start=1): # 1-based index
...
Parallel iteration: zip
for a, b in zip(xs, ys): # stops at shortest
...
for a, b in zip(xs, ys, strict=True): # ValueError if lengths differ
...
Reverse iteration
for x in reversed(nums): # O(1) extra space, no copy
...
for x in nums[::-1]: # makes a copy, O(n) space
...
Dict iteration
for k in d: # keys
...
for k, v in d.items(): # key-value pairs
...
for v in d.values():
...
2D grid
for r in range(len(grid)):
for c in range(len(grid[0])):
...
for r, row in enumerate(grid):
for c, val in enumerate(row):
...
for-else (else runs only if the loop didn't break)
for x in nums:
if x == target:
print("found")
break
else:
print("not found")