# Process only valid records
records = [
{"name": "Alice", "age": 30},
{"name": "", "age": 25},
{"name": "Bob", "age": -1},
{"name": "Charlie", "age": 35},
]
valid = []
for record in records:
ifnot record["name"]:
continueif record["age"] < 0:
continue
valid.append(record)
print(f"Valid records: {len(valid)}")
for r in valid:
print(f" {r['name']}, age {r['age']}")
Output
Click "Run" to execute your code
continue is useful for guard clauses at the top of a loop body, keeping the main logic un-indented and readable.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?