Filtering and Processing Data from Files
continue means stop processing this item and move to the next loop iteration. Statements later in the current iteration do not run.
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)- 1The loop obtains a line from the file.
- 2The condition checks whether the line does not start with From:.
- 3If that condition is true, continue skips the print statement and begins the next iteration.
- 4If the condition is false, the line starts with From: and reaches print.
- Each iteration examines one line from the file.
- A line that does not start with From: is uninteresting and is skipped.
- Only lines that start with From: reach the print statement.
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
Common mistake Treating continue as if it exits the entire loop.
What it looks likecontinue skips the rest of the current iteration and returns to the loop start.
Why it failsThe loop continues examining later lines.
Do this insteadUse continue when only the current item should be skipped. The source distinguishes this from break, which exits the loop entirely.
Common mistake Assuming that a line after continue will still be processed.
What it looks likeA line that does not start with From: reaches continue before the print statement.
Why it failsAll statements after continue in that iteration are skipped.
Do this insteadTrace the control flow from continue directly to the next iteration.
Common mistake Filtering the wrong set of lines by overlooking not.
What it looks likenot line.startswith("From:") identifies lines that do not start with From:.
Why it failsThe condition is designed to find uninteresting lines to skip, not matching lines to process.
Do this insteadAsk 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 |
The source distinguishes continue from break by whether later iterations can begin.
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:.
- 1First line
From: team@example.com starts with From:, so continue does not run and the line reaches processing.
- 2Second line
Subject: Update does not start with From:, so continue runs and the line is skipped.
- 3Third 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
- 1continue skips the rest of the current loop iteration and starts the next iteration.
- 2The pattern if not condition: continue identifies uninteresting lines and filters them before processing.
- 3Only lines that do not trigger continue reach the statements below the filter.
- 4The contracted if form places the condition and continue action on one line and behaves like the equivalent indented form.
- 5Tracing 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.