Find and count
Scan a sequence to locate items and tally occurrences — the most fundamental building block in algorithms.
- Implement a linear scan to find the first item matching a condition
- Implement a count that tallies how many items satisfy a condition
- Recognise find-and-count as the base pattern underlying most list algorithms
Every algorithm that works on a list does one of a small number of things. The most basic — so basic it barely feels like a pattern — is simply looking at each item once. That's a linear scan: start at the beginning, examine every element in order, stop when you have what you need.
Two shapes come out of this single idea:
- Find first — scan and return the moment you find what you're looking for.
- Count all — scan the whole list and tally up how many items match.
These sound trivial. They're not. Once you can name them, you'll recognise them inside almost every list algorithm you'll ever write.
Find first
Finding the first item that satisfies a condition is a common need. The key insight: return early as soon as you hit a match. There's no reason to keep looking.
def find_first_even(nums):
for n in nums:
if n % 2 == 0:
return n
return None # nothing matchedThe return None at the end is important. A function with no explicit return
gives you None anyway, but writing it out makes the contract clear: "I
searched the whole list and found nothing."
Early return is what separates "find first" from "count all". The moment you have your answer you leave — no wasted work scanning the rest of the list.
Count all
Counting requires seeing every element: you can't know the final tally until you've looked at everything. The pattern is a counter that starts at zero and increments whenever a condition is met.
def count_negatives(nums):
count = 0
for n in nums:
if n < 0:
count += 1
return countThere's no early exit here. Even if you find a negative number on the very first iteration, you keep going — more negatives might follow.
The counter pattern (count = 0 before the loop, count += 1 inside) is
one of the first things to reach for when a problem asks "how many". It is
the basis for more sophisticated techniques — like building a dictionary of
counts — that you'll see in the intermediate tier.
Seeing both patterns side by side
Run the code, then try changing numbers to see how both functions respond.
Notice that find_first_even stops the moment it finds 2 — it never looks
at -8 or 5.
Why bother naming these patterns?
You will write variations of find-first and count-all constantly. Recognising the shape of a problem — "this is a count, I need to touch every element" vs. "this is a find, I can stop early" — stops you from reaching for more complex tools when a simple loop is all you need.
Write count_above(nums, threshold) that returns how many numbers in nums are strictly greater than threshold.
count_above([1, 5, 3, 8, 2], 4) → 2Where to go next
Next: filter and transform — instead of just counting items, you'll start building new lists from old ones.