Filtering and Processing Data from Files
The continue statement skips the rest of the current loop iteration and jumps back to the loop start, enabling efficient filtering of uninteresting data.
Why Skip Lines Early
When a loop examines lines from a file, not every line may be relevant to the task. A search loop can test each line and immediately skip the uninteresting ones. The continue statement makes this possible: it stops the current iteration and sends execution back to the start of the loop, where the next line can be examined.
continue means stop processing this item and move to the next loop iteration. Statements later in the current iteration do not run.
The Filtering Pattern
A clear search loop first identifies the lines that should be ignored. The common pattern is if not condition: continue. The condition describes an interesting line, while not condition identifies every line that is uninteresting. Those lines are skipped immediately. If the condition is true, continue does not run, so the statements below the test can process the line.
for line in file: if not line.startswith("From:"): continue print(line)
A Three-Line Trace
Trace the loop with a small file containing three lines: Subject: Meeting, From: alice@example.com, and Date: 2024-01-15. The loop tests each line in order. The first and third lines do not start with From:, so continue skips them. The middle line matches and reaches print.
| Input line | Condition result | Action |
|---|---|---|
| Subject: Meeting | Does not start with From: | continue skips processing |
| From: alice@example.com | Starts with From: | Reaches print |
| Date: 2024-01-15 | Does not start with From: | continue skips processing |
Execution trace for the three-line sample file.
From: alice@example.comCompact and Expanded Syntax
Python permits an if statement and its action to appear on one line. In a search loop, the contracted form keeps the skip condition and continue action together. It has the same behavior as the equivalent indented form; the difference is only how the statement is written.
Mistakes in Search Loops
Treating continue as if it exits the entire loop.
The loop continues examining later lines.
Fix:
Use continue when only the current item should be skipped. The source distinguishes this from break, which exits the loop entirely.Assuming that a line after continue will still be processed.
All statements after continue in that iteration are skipped.
Fix:
Trace the control flow from continue directly to the next iteration.Filtering the wrong set of lines by overlooking not.
The condition is designed to find uninteresting lines to skip, not matching lines to process.
Fix:
Ask which lines make the condition true. Those lines execute continue and never reach the processing statement.
| Statement | Effect on current iteration | Effect on loop |
|---|---|---|
| continue | Skips the remaining statements | Moves to the next iteration |
| break | Stops the current iteration | Exits the loop entirely |
Practice the Trace
Suppose a file contains these lines in order: From: team@example.com, Subject: Update, From: support@example.com. Using the filtering pattern, identify which lines reach the processing statement and which lines are skipped by continue.
Hints
- A line reaches processing only when it starts with From:.
- For every other line, continue skips the remaining statements in that iteration.
- Preserve the original order of the lines that are processed.
Tracing the Practice Input
Determine which lines are processed by a loop that skips every line that does not start with From:.
First line: From: team@example.com starts with From:, so continue does not run and the line reaches processing.
Second line: Subject: Update does not start with From:, so continue runs and the line is skipped.
Third line: From: support@example.com starts with From:, so the line reaches processing.
The processed lines are From: team@example.com and From: support@example.com. Subject: Update is skipped.
Working Rule
- continue skips the rest of the current loop iteration and starts the next iteration.
- The pattern if not condition: continue identifies uninteresting lines and filters them before processing.
- Only lines that do not trigger continue reach the statements below the filter.
- The contracted if form places the condition and continue action on one line and behaves like the equivalent indented form.
- Tracing each input line separately reveals exactly which lines are skipped and which are processed.
Key Takeaways
- Use continue to stop processing the current line and move to the next loop iteration.
- Use if not condition: continue when it is clearer to describe the lines that should be skipped.
- Only lines that pass the filter reach the processing statements below it.
- The contracted if syntax is a compact equivalent of the multi-line if statement.
- A line-by-line trace can verify the control flow of a file-search loop.