Filtering Data with For Loops

Introduction: Not Every Item Deserves a Reaction

Welcome to the final unit of Iterating with For Loops in Python!

Unit 3 gave our loops memory through counters and accumulators. Those loops treated every element the same way: every score joined the total, and every element increased the count.

Most real questions are more selective. Consider a list of recorded temperatures:

temperatures = [68, 75, 82, 59, 90, 71]

We might want to know:

  • How many days were hot?
  • What is the total of only the comfortable temperatures?
  • Which temperatures were below the comfortable range?
  • How can we track two categories during one pass?

By the end of this lesson, we will combine the loops from earlier units with the if statements you already know.

The Shape of a Filtered Loop: Two Levels of Indentation

An if inside a for creates a loop that visits every item but reacts only to matching items:

for temp in temperatures:
    if temp >= 80:
        print(temp)

Read the indentation carefully:

  • for temp in temperatures: is the loop header.
  • if temp >= 80: is inside the loop, so the condition is tested once per temperature.
  • print(temp) is inside the if, so it runs only when the condition is True.
Flowchart showing every temperature being tested and only matching temperatures being printed

The loop visits 68, 75, 82, 59, 90, and 71. Only 82 and 90 pass the test, so only those two values are printed.

Counting Matches: The Filtered Counter

Counting hot days combines the counter pattern from Unit 3 with a condition:

hot_days = 0

for temp in temperatures:
    if temp >= 80:
        hot_days += 1

The counter begins before the loop. During each iteration, the if acts as a gate:

  • If temp >= 80 is True, the counter increases.
  • If the condition is False, the counter stays unchanged.

The loop still visits every value. Only matching values contribute to the result.

Tracing the Filter Iteration by Iteration

Here is the actual counter state after each iteration:

Iterationtemptemp >= 80hot_days after
168False0
275False0
382True1
459False1
590True2
671False2

A failed condition does not reset the counter. It simply leaves the existing value unchanged. That is why hot_days remains 1 during the fourth iteration.

After the loop, its final value is 2.

Summing Only What Passes: The Filtered Accumulator

The same gate works with an accumulator. Suppose comfortable temperatures range from 65 through 78, including both boundaries:

comfortable_total = 0

for temp in temperatures:
    if 65 <= temp <= 78:
        comfortable_total += temp

The condition:

65 <= temp <= 78

is a chained comparison. It means the same thing as:

temp >= 65 and temp <= 78

The qualifying temperatures are 68, 75, and 71. Therefore:

68 + 75 + 71 = 214

The only difference from a filtered counter is the update:

  • += 1 counts matching items;
  • += temp sums the values of matching items.

Both boundaries are inclusive because the condition uses <=.

Number line showing comfortable temperatures from 65 through 78 and hot temperatures from 80 upward

Printing Each Matching Item

Sometimes we do not need a final count or total. We simply want to print each item that matches a condition:

for temp in temperatures:
    if temp < 65:
        print("Below comfortable range:", temp)

Only 59 passes the test, so the output is:

Below comfortable range: 59

Here, the print() belongs inside the if because we want one output line for each match.

Compare the two patterns:

# Print every match during the loop.
for temp in temperatures:
    if temp < 65:
        print(temp)
# Build one final count and print it after the loop.
cold_days = 0

for temp in temperatures:
    if temp < 65:
        cold_days += 1

print(cold_days)

The correct location of print() depends on whether we want each matching value or one final summary.

Maintaining Two Counters in One Loop

A single loop can maintain multiple filtered counters:

comfortable_days = 0
hot_days = 0

for temp in temperatures:
    if 65 <= temp <= 78:
        comfortable_days += 1
    if temp >= 80:
        hot_days += 1

print("Comfortable days:", comfortable_days)
print("Hot days:", hot_days)

Both counters are initialized before the loop. During each iteration, Python checks both conditions.

The two if statements are independent:

if 65 <= temp <= 78:
    comfortable_days += 1

if temp >= 80:
    hot_days += 1

An independent pair of if statements means, “Check the first condition, and also check the second condition.”

This differs from if/elif:

if 65 <= temp <= 78:
    comfortable_days += 1
elif temp >= 80:
    hot_days += 1

With if/elif, Python checks the elif only when the first condition is false. That structure is appropriate when selecting one mutually exclusive branch.

For comfortable and hot temperatures, both versions happen to produce the same counts because one temperature cannot be in both ranges. However, two independent if statements communicate that we are tracking two separate measurements. They also continue to work if categories are allowed to overlap.

For example:

positive_numbers = 0
even_numbers = 0

for number in [2, -4, 5, 8]:
    if number > 0:
        positive_numbers += 1
    if number % 2 == 0:
        even_numbers += 1

The number 2 is both positive and even, so both counters should increase. Using if/elif would incorrectly prevent the second update after the first condition succeeded.

Full Program and Output

Here is the basic hot-day and comfortable-total program:

temperatures = [68, 75, 82, 59, 90, 71]

hot_days = 0

for temp in temperatures:
    if temp >= 80:
        hot_days += 1

comfortable_total = 0

for temp in temperatures:
    if 65 <= temp <= 78:
        comfortable_total += temp

print("Hot days:", hot_days)
print("Comfortable total:", comfortable_total)

Both print() calls are after the loops because the answers are final only after every temperature has been checked.

Hot days: 2
Comfortable total: 214

The two calculations could also be performed in one pass:

hot_days = 0
comfortable_total = 0

for temp in temperatures:
    if temp >= 80:
        hot_days += 1
    if 65 <= temp <= 78:
        comfortable_total += temp

Both forms are valid. The one-loop version avoids visiting the same list twice and keeps related measurements together.

Common Pitfalls When Filtering Inside Loops

Filtered loops fail in a handful of predictable ways:

  • Incorrect indentation: if the update is aligned with the if instead of nested inside it, every item contributes.
  • Putting the if after the loop: only the last value held by the loop variable is tested.
  • Using = instead of ==: equality tests require ==.
  • Choosing the wrong boundary: temp > 80 excludes exactly 80, while temp >= 80 includes it.
  • Resetting inside the loop: placing hot_days = 0 in the loop erases previous matches.
  • Printing a summary inside the loop: this displays partial results rather than one final answer.
  • Using elif for overlapping categories: only the first matching branch runs, even when both counters should increase.

A result of 0 is not automatically an error. It may correctly mean that no item satisfied the condition.

When a filtered result looks wrong, check the condition and the indentation of the update first.

Conclusion and Next Steps

The filtered-loop pattern fits into four steps:

  1. Initialize a variable before the loop.
  2. Visit every item.
  3. Test each item with an if.
  4. Update only when the condition matches.

Everything else comes from the earlier units. The if simply controls which items are allowed to contribute.

Congratulations on reaching the end of Iterating with For Loops in Python! We started by visiting items one at a time, generated integer progressions with range(), gave loops memory with counters and accumulators, and taught them to be selective with conditions.

The final practices will count values above a threshold, sum values inside a range, print matching elements, and track two conditions during one loop.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal