continue — Advanced Playground
Skips the rest of the current loop iteration and moves to the next
Python Playground
data = ["42", "hello", "17", "", "99", "3.14", "7"]
numbers = []
for item in data:
try:
numbers.append(int(item))
except ValueError:
continue
print(f"Parsed numbers: {numbers}")
# Continue with nested context
matrix = [[1, 0, 3], [0, 5, 6], [7, 8, 0]]
for i, row in enumerate(matrix):
for j, val in enumerate(row):
if val == 0:
continue
print(f"({i},{j})={val}", end=" ")
print()
Output
Click "Run" to execute your code
continue combined with try/except lets you skip invalid items in a loop gracefully without stopping the entire iteration.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?