Concepts / Filtering and Processing Data from Files

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.

  • Programming

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.

examine lineuninterestingjumpinterestingLoop iterationLine testcontinueskip current iterationNext iterationProcessing statement
When continue executes, where does control flow go, and which statements in the current iteration are skipped?

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)

testnoyesFile lineStarts with From:conditioncontinueuninteresting lineprint linematching line
How does each line move through the condition so that uninteresting lines are skipped and matching lines are processed?

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 lineCondition resultAction
Subject: MeetingDoes not start with From:continue skips processing
From: alice@example.comStarts with From:Reaches print
Date: 2024-01-15Does not start with From:continue skips processing

Execution trace for the three-line sample file.

skipafter processingskipSubject: MeetingcontinueNext iterationFrom:alice@example.comprintDate: 2024-01-15continue
For each input line, does execution skip the processing step or continue to it, and what happens next?
Output
From: alice@example.com

Compact 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.

python
python
equivalentequivalentContracted ifcondition and continue onone lineSame loop behaviorIndented ifcondition and continue onseparate lines
Which file lines are discarded by the filter, and which reach the processing code in both syntax forms?

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.

StatementEffect on current iterationEffect on loop
continueSkips the remaining statementsMoves to the next iteration
breakStops the current iterationExits the loop entirely

Practice the Trace

EASY

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

  1. continue skips the rest of the current loop iteration and starts the next iteration.
  2. The pattern if not condition: continue identifies uninteresting lines and filters them before processing.
  3. Only lines that do not trigger continue reach the statements below the filter.
  4. The contracted if form places the condition and continue action on one line and behaves like the equivalent indented form.
  5. 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.