AP® Computer Science Principles · Big Idea 2 · Topic 2.4

Filtering and Transforming Data with a Program

Filtering walks a data set and keeps only records that pass a test, shrinking the list. Transforming walks every record and changes it, keeping the list the same size. Doing them in a different order can change the final result.

Filtering and transforming are both operations that process a data set one record at a time, but they do different jobs. A filter asks a yes-or-no question of each record, something like "is this value greater than 5?", and builds a new, usually shorter list out of only the records that answer yes. The records themselves never change; some just get dropped. A transform, by contrast, touches every single record and changes it in some way, like doubling every number, but keeps the same number of records you started with. This is why filtering and sorting are so different too: sorting rearranges every record without removing or changing any of them, while filtering removes records outright based on a test.

Because filtering shrinks the list and transforming changes the values, the order you apply them in can change the final answer. If you transform first and then filter, the filter test is applied to the new, changed values. If you filter first and then transform, the filter test is applied to the old, original values, which can let in or throw out different records than you intended. Once a record is filtered out, it is gone for good, so an early filter based on the wrong values can permanently discard something that should have survived.

This matters because real programs chain these steps together constantly, such as cleaning a spreadsheet of sensor readings before graphing it. Getting the order backwards is a quiet bug: the program still runs and still produces a list, just the wrong one, which makes this an easy mistake to miss without checking a small example by hand.

Key terms

Filter
An operation that tests every record in a data set and keeps only the ones that pass, discarding the rest.
Transform
An operation that changes every record in a data set in the same way while keeping the total count unchanged.
Record
A single entry or item within a data set, such as one number in a list.
Test condition
The yes-or-no rule a filter applies to each record to decide whether it is kept.

Watch it explained

  1. How it worksHow does a filter walk an entire data set and keep only the records that pass a test?
  2. How it worksWalk me through transforming every element of a data set by doubling it in place.
  3. Worked exampleFind the result of filtering the list 4, 9, 2, 15, 7 to keep only values greater than 5.
  4. Going furtherWhat happens to a result when you filter first and then transform instead of the other way round?

Check yourself

Your turn1 of 2
A data set has 1408 records. A filter keeps only the records passing a test, and 63 percent of the records pass. How many records does the filter keep?

End of topic.