Looping problems usually involve lists, minimums, maximums, counting, and/or summation. A few examples might be

  • Given a list of people and salaries, who makes the most money?
  • Given a list of restaurant reviews, what percentage are above 4 stars?
  • Given a list of movie ratings, which got the worst score?
  • Given a list of budget items, what is the total budget for cleaning supplies?

NOTE: To be perfectly honest, there are a lot of other ways of doing these problems besides for loops, and most all of them are actually better! This is a good way to think through it for beginning Python students, though.

Overview

You have three steps to any sort of problem that involves loops or lists.

  1. The Initial Condition: You need to start somewhere. For example: If I’m counting dogs, how many dogs do I know about before I even start counting? Zero, probably.
  2. The Conditional: Every time you go through the loop, you probably test something to see if you should make a change. If I’m counting pit bulls, it might be something like is this a pit bull? if it is, let’s do something.
  3. The Change: If the conditional is true, I need to do something. Maybe it’s since this was a pit bull, let’s incrementing the pit_bulls counter.

It doesn’t count as a step, but there’s also The Summary, where you display your answer.

Type 1: Counting

Definition: Given a list of numbers/objects/etc, how many are there?

  1. The Initial Condition: You need a counter. The counter starts at zero, because you haven’t counted anything yet.
  2. The Conditional: NONE, because we count all of them
  3. The Change: Add one to your counter.

In words

Data set: We have a list of dogs, how many dogs are there?

Initial condition: We haven’t counted any pit bulls yet, so we’ve only counted 0.

The loop: We walk down the line of dogs, stopping at each one. Each time we visit a dog, we make a little counting mark on our page (the change).

The summary: When we’re done, we look at how many marks we’ve made on our page.

In code

Let’s find out how many numbers we have.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted, we've found ZERO numbers greater than 100
large_numbers_count = 0

# THE LOOP: Go through each one of our numbers...
for number in numbers:
  # THE CONDITIONAL: We don't have one, since we count all of them
  # THE CHANGE: Let's increment our counter
  numbers_count = numbers_count + 1

# THE SUMMARY: print it out
print("We found", numbers_count, "numbers")

Sometimes it’s clearer without all of the comments, actually! Here’s the same code again.

numbers = [5, 20, 377, 123, 1, 384, 0]

numbers_count = 0

for number in numbers:
  numbers_count = numbers_count + 1

print("We found", numbers_count, "numbers")

Of course, len is a much better option for this.

Type 2: Conditional counting

Definition: Given a list of numbers/objects/etc, how many fulfill some sort of condition?

  1. The Initial Condition: You need a counter. The counter starts at zero, because you haven’t counted anything yet.
  2. The Conditional: You examine your item and think, should you count this item?
  3. The Change: Add one to your counter.

In words

Data set: We have twenty dogs, and are keeping track of how many pit bulls there are.

Initial condition: We haven’t counted any pit bulls yet, so we’ve only counted 0.

The loop: We walk down the line of dogs, stopping at each one. Is that dog a pit bull? (conditional) - if it is, we make a little counting mark on our page (the change).

The summary: When we’re done, we look at how many marks we’ve made on our page.

In code

Let’s find out how many of our numbers are greater than 100.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted, we've found ZERO numbers greater than 100
large_numbers_count = 0

# THE LOOP: Go through each one of our numbers...
for number in numbers:
  # THE CONDITIONAL: check to see if the number is larger than 100
  if number > 100:
    # THE CHANGE: since the number is greater than 100, let's increment our counter
    large_numbers_count = large_numbers_count + 1

# THE SUMMARY: print it out
print("We found", large_numbers_count, "numbers larger than 100")

Sometimes it’s clearer without all of the comments, actually! Here’s the same code again.

numbers = [5, 20, 377, 123, 1, 384, 0]

large_numbers_count = 0

for number in numbers:
  if number > 100:
    large_numbers_count = large_numbers_count + 1

print("We found", large_numbers_count, "numbers larger than 100")

Type 3: Summation/Totaling

Definition: Given a list of numbers, what is their total?

  1. The Initial Condition: You need a total. The total starts at zero, because you haven’t added anything yet.
  2. The Conditional: NONE, you’re adding all of them up
  3. The Change: Add the value to your running total.

In words

Data set: We have the population of each states in America, and are keeping track of how many people live in this country.

Initial condition: We haven’t counted anyone yet, so our total is 0.

The loop: We go through each state, looking at each one. Each time we visit a state, we add its population to our running total (the change).

The summary: When we’re done, we see what our total is.

In code

Let’s find out what the sum of all our numbers is.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted anything, our total is 0
total = 0

# THE LOOP: Go through each one of our numbers...
for number in numbers:
  # THE CHANGE: and add it to our total
  total = total + number

# THE SUMMARY: print it out
print("The sum of our numbers is", total)

Sometimes it’s clearer without all of the comments, actually! Here’s the same code again.

numbers = [5, 20, 377, 123, 1, 384, 0]

total = 0

for number in numbers:
  total = total + number

print("The sum of our numbers is", total)

Type 4: Conditional Summation/Totaling

Definition: Given a list of numbers, what is the total of the ones that meet a certain condition?

  1. The Initial Condition: You need a total. The total starts at zero, because you haven’t added anything yet.
  2. The Conditional: You examine your item and think, should I add this one to my total?
  3. The Change: Add the value to your running total.

In words

Data set: We have the population of each states in America, and are keeping track of how many people live in Republican-voting states.

Initial condition: We haven’t counted anyone yet, so our total is 0.

The loop: We go through each state, looking at each one. Did it vote Republican in the last election? If so, we add its population to our running total (the change).

The summary: When we’re done, we see what our total is.

In code

Let’s find out what the sum of all our even numbers is.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted anything, our total is 0
total = 0

# THE LOOP: Go through each one of our numbers...
for number in numbers:
  # THE CONDITIONAL: Is it an even number?
  if number % 2 == 0:
    # THE CHANGE: if it is, add it to our total
    total = total + number

# THE SUMMARY: print it out
print("The sum of the even numbers is", total)

Sometimes it’s clearer without all of the comments, actually! Here’s the same code again.

numbers = [5, 20, 377, 123, 1, 384, 0]

total = 0

for number in numbers:
  if number % 2 == 0:
    total = total + number

print("The sum of the even numbers is", total)

Type 4: Largest/Smallest numbers

Definition: Given a list of numbers, which is the largest/smallest?

  1. The Initial Condition: You need a current known largest. We don’t know which is the largest, so we’ll start off with the first one.
  2. The Conditional: You examine your item and think, is this bigger than any number I’ve seen before?
  3. The Change: If it is, save this new number as your biggest number.

In words

We have four people with certain amounts of money:

  • our cat ($0)
  • Kanye West ($300)
  • Bill Gates ($900), and
  • the mailman ($200)

The question is, who is the richest?

Initial Condition: Before we start, we’re write down “CAT, $0” as the richest person we know. Now we’re going to visit all four people (the loop).

  • First, we visit our cat. Is our cat ($0) richer than our cat ($0)? (conditional) Well… no, I guess. So we move on.
  • Next, we visit Kanye. Is Kanye ($300) richer than our cat ($0)? Yes! The change: So now we scratch off our cat’s info and write “KANYE, $300” as the richest.
  • Next, we visit Bill Gates. Is Bill Gates ($900) richer than Kanye ($200)? Yes! So now we scratch off Kanye’s info and write “BILL GATES, $900” as the richest.
  • Next, we visit the mailman. Is the mailman ($200) richer than Bill Gates ($900)? No, so we move on.

Summary: Finally, we look at the name we’ve written down as the richest: BILL GATES, $900.

NOTE: Be sure to keep track of the right amount of information! If we had just kept track of the richest person’s name in the example above, we wouldn’t have been able to compare how rich they were in the conditional. We needed to keep track of their name to display at the end, along with their wealth to do the comparison.

In code

Let’s find out what the largest of all our numbers is.

# This is our data set
numbers = [5, 20, 377, 123, 1, 384, 0]

# THE INITIAL CONDITION: Before we've counted anything, our largest is the first element
largest = numbers[0]

# THE LOOP: Go through each one of our numbers...
for number in numbers:
  # THE CONDITIONAL: Is it larger than our previous largest?
  if number > largest:
    # THE CHANGE: if it is, save it as the new largest
    largest = number

# THE SUMMARY: print it out
print("The largest number we've seen is", largest)

Sometimes it’s clearer without all of the comments, actually! Here’s the same code again.

numbers = [5, 20, 377, 123, 1, 384, 0]

largest = numbers[0]

for number in numbers:
  if number > largest:
    largest = number

print("The largest number we've seen is", largest)

Others

There are a lot more! Maybe we’ll add then as time goes by.

Note: Generally speaking, you’re doing this with lists of dictionaries, not lists of numbers, so your actual code will be a little more complicated.