6: Iteration#

What are loops and why should we care about them?#

Loops are a fundamental building block of comptutational solutions to problems. They are an example of a control structure. Conditionals are another example of control structures.

Control structures allow you to control when/whether (conditional) and how many times (loops) you do things

It’s hard to build programs without a concise way to instruct the computer to do repeated actions.

Here are some simple examples. Try to think of how you might solve these without loops!

  • Put 6 cups of flour into a box

  • Stir occasionally until the sauce starts to reduce

With loops these get a LOT easier to specify, and become more robust and reusable too.

Example (not real!) program: Put 6 cups of flour into a box

1def scoop_into_box():
2    print("Scoop into box")
3
4# scoop into the box 6 times
5for i in range(6):
6    scoop_into_box()
Scoop into box
Scoop into box
Scoop into box
Scoop into box
Scoop into box
Scoop into box

Example (not real!) program: Stir occasionally until the sauce starts to reduce (i.e., is thick)

1# stir the sauce until it is thick
2while check_sauce() == "thick":
3    stir()

Loops also enable many useful algorithms/patterns that go nicely with lists. You’ll be practicing and applying them in PCEs and Projects this module!

For example:

  • Searching through a list

  • Filtering a list of items

  • Counting occurrences in some collection

Continuing with our running example for this module, here are loops in the context of a program:

 1# key variables:
 2# the input LIST of strings
 3inputs = [
 4    "hello sarah@umd.edu",
 5    "from: joelchan@umd.edu",
 6    "some other text that doesn't have an email"
 7]
 8# a LIST to store the email addresses
 9emails = []
10
11# LOOP over every text input
12for text_input in inputs:
13    
14    # extract an email address
15    # split the text into subsets
16    chunks = text_input.split()
17    
18    # LOOP over the list of chunks to check each one
19    for chunk in chunks:
20        # check if it has @ and .
21        if "@" in chunk and "." in chunk:
22            # put the chunk in the email list
23            emails.append(chunk)
24# give the email address back to the user
25print(emails)
['sarah@umd.edu', 'joelchan@umd.edu']

Definite loops (for loops)#

Quite often we have a list of items of the lines in a file - effectively a finite set of things. We can write a loop to do some operation once for each of the items in a set using the Python for construct.

These loops are called “definite loops” because they execute an exact number of times. We say that “definite loops iterate through the members of a set”

Use definite/for when you know in advance how many times you want to do something.

This is the use case in our running example.

Other examples:

  • Do an action N times

  • Take M steps

  • Do something for every item in a finite list

Anatomy of a definite (for) loop in Python#

Let’s take a closer look.

  • The iteration variable “iterates” through the sequence (ordered set)

  • The block (body) of code is executed once for each value in the sequence

  • The iteration variable moves through all of the values in the sequence

 1nums = [5, 4, 3, 2, 1]
 2# here, n is the iteration variable
 3
 4n = nums[0]
 5n = nums[1]
 6n = nums[2]
 7
 8for n in nums: 
 9    print("taking something from the list")
10    print(n) # block/body
1nums = [5, 4, 3, 2, 1]
2# here, i is the iteration variable
3for i in nums: 
4    print(i)
5    new_num = i*20
6    print(new_num) # block/body
5
100
4
80
3
60
2
40
1
20

The iteration variable is a variable: this means you can name it whatever you like, subject to the basic syntax rules and of course our heuristic to name things to make the logic of the program legible.

What is the iteration variable here?

1nums = [5, 4, 3, 2, 1]
2for a_number in nums: 
3    new_num = a_number*20
4    print(new_num) # block/body

What is the iteration variable here?

1nums = [5, 4, 3, 2, 1]
2for num in nums:
3    if num % 2 == 0: # check if even
4        print(num) # block/body

What is the iteration variable here?

1for name in ["john", "terrell", "qian", "malala"]:
2    print(name)
1# the range function produces an iterable sequence of numbers
2# that start at the optional first argument, and stop before
3# the required second argument
4# https://www.w3schools.com/python/ref_func_range.asp
5list(range(0,5))
1# use this if you want to specify doing something N times
2# e.g., here, take a step 5 times
3for i in range(7):
4    print("I has the value", i)
5    print("Taking a step")
1# use this if you want to specify doing something N times
2# e.g., here, take a step 5 times
3for i in [0,1,2,3,4]:
4    print("I has the value", i)
5    print("Taking a step")
1# scoop 6 cups
2steps = 6
3for step in range(steps):
4    print("scooping cup number", step+1)

PRACTICE: how would you write a loop to print “hello” 3 times?

1# practice: your code here

PRACTICE: print out each name in this list

1names = ["Joel", "John", "Jane", "Jamie", "John", "Michael", "Sarah", "Joseph", "Chris", "Ray"]
2
3# your loop here

PRACTICE: print out each donation in this list

1donations = [
2    0.00, 10.00, 25.00, 50.00, 75.00, 100.00, 250.99, 500.00, 1000.00, 2500.00,  
3    5000.00, 7500.50, 10000.00, 0.00, 12500.75, 15000.00, 20000.99, 25000.00, 30000.00,  
4    40000.00, 50000.00, 243.29, 0.00  
5]
6
7
8# your loop here

To get a feeling for what is going on, try copy-pasting one of these programs into python tutor and inspect it!

Common design patterns with definite loops#

Counting#

A common situation: you have a list of stuff, and you want to count how many times a certain kind of thing shows up in that list.

If you want to count occurrences based on a simple exact match, you can use the .count() list method.

1names = ["Joel", "John", "Jane", "Jamie", "John", "Michael", "Sarah", "Joseph", "Chris", "Ray"]
2# count how many times "John" is in here
3names.count("John")

But often you want to count based on something more complex than an exact match. For example, let’s say I want to count the number of “high performers” in a list of scores (where high performing means score of 95 or greater).

Iteration is a really helpful way to do this.

Here’s an example program for counting how many scores are above a user-defined threshold.

 1# 
 2scores = [65, 78, 23, 97, 100, 25, 95] # input list
 3
 4threshold = 67 # score of C
 5
 6n_highperformers = 0 # define the count variable, initialize to 0
 7
 8# go through each item
 9for score in scores: 
10    # check if meets my criteria for being counted
11    if score >= threshold: 
12        # if so, increase count
13        n_highperformers += 1 
14print(n_highperformers)
4

The generic pattern (or algorithm) is something like this:

1# initialize count variable
2
3# for every item in list
4    # check if meets my criteria for being counted
5        # if so, increase count

PRACTICE: count how many “small dollar donors” (less than 100) or “big dollar donors” (1000 or up) are in this list of donations. Be careful! 0 is not a small dollar donation!

1donations = [
2    0.00, 10.00, 25.00, 50.00, 75.00, 100.00, 250.99, 500.00, 1000.00, 2500.00,  
3    5000.00, 7500.50, 10000.00, 0.00, 12500.75, 15000.00, 20000.99, 25000.00, 30000.00,  
4    40000.00, 50000.00, 243.29, 0.00  
5]

PRACTICE: count how many names are short (e.g., 4 characters or less)? Or long (e.g., more than 5 characters).

1names = ["Joel", "John", "Jane", "Jamie", "Johnny", "Michaela", "Sarah", "Joseph", "Chris", "Ray"]
2
3# your code here

PRACTICE: check how many times we have a “banned” name. *hint: how do we check if an item is in a list?

1names = ["Joel", "John", "Jane", "Jamie", "Johnny", "Michaela", "Sarah", "Joseph", "Chris", "Ray"]
2
3banned = ["Joel", "Chris"]
4
5# your code here

Filtering#

A closely related situation is where we want to not only count, but also “grab” or filter the things that meet our criteria.

We’d want to create a new list, and make sure we have a bit of code that adds to that new list based on the criteria we have.

Example: grab all scores that cross our threshold.

 1scores = [65, 82, 23, 97, 100, 95] # input list to be filtered
 2
 3threshold = 93 # the criterion
 4
 5# initialize empty list to hold filtered items
 6to_grab = [] 
 7
 8# go through each item
 9for score in scores: 
10    # check if item meets criteria for being filtered
11    if score >= threshold: 
12        # if so, add the item to the output list
13        to_grab.append(score) 
14
15print(to_grab)
[97, 100, 95]

The generic pattern is something like this:

1# initialize empty list to hold filtered items
2
3# go through each item
4    # check if item meets criteria for being filtered
5        # if so, add the item to the output list

PRACTICE: Let’s modify our program above to grab the small dollar donations and put them in a new list so we can count how many we have and what the total and average small dollar donation is.

1donations = [
2    0.00, 10.00, 25.00, 50.00, 75.00, 100.00, 250.99, 500.00, 1000.00, 2500.00,  
3    5000.00, 7500.50, 10000.00, 0.00, 12500.75, 15000.00, 20000.99, 25000.00, 30000.00,  
4    40000.00, 50000.00, 243.29, 0.00  
5]

PRACTICE: Let’s modify our program above to grab only the names that aren’t in our banned list.

1names = ["Joel", "John", "Jane", "Jamie", "Johnny", "Michaela", "Sarah", "Joseph", "Chris", "Ray"]
2
3banned = ["Joel", "Chris"]
4
5# your code here

Just a reminder that you can use the filter() function if you’re curious, BUT YOU DO NOT HAVE TO. This is just an extra thing if you’re curious

1scores = [65, 82, 23, 97, 100, 95] # input list to be filtered
2threshold = 80
3
4def meets_critera(x):
5    return x >= threshold
6
7to_grab = list(filter(meets_critera, scores))
8print(to_grab)

Mapping / transforming#

Finally, sometimes you want to modify some/all elements in a list into a new list. An example might be data cleaning, or data transformation.

EXAMPLE:: Convert a list of scores (on scale of 0 to 100) to proportions.

 1# input list
 2scores = [65, 82, 23, 97, 100, 95]
 3
 4# output list
 5proportions = []
 6
 7# go through every item
 8for score in scores:
 9    # apply the transformation
10    proportion = score/100
11    # add the transformed value to the output list
12    proportions.append(proportion)
13proportions
[0.65, 0.82, 0.23, 0.97, 1.0, 0.95]

A variant of the program that’s a bit more concise (does the same thing):

 1# input list
 2scores = [65, 82, 23, 97, 100, 95]
 3
 4# output list
 5proportions = []
 6
 7# go through every item
 8for score in scores:
 9    # apply the transformation
10    # add the transformed value to the output list
11    proportions.append(score/100)
12proportions

The generic pattern is something like this

1# initialize empty list to hold transformed items
2
3# go through each item in the input list
4    # apply transformation to item
5    # add transformed item to transformed items list

PRACTICE: Change outliers (those above 1000) to missing (“NA”)

1scores = [65, 82, 2323, 97, 100, 95000]
2
3# your code here

PRACTICE: Change the list from scores to letter grades, using the score to letter grade mappings from our syllabus (e.g., 93 and above is A).

1scores = [65, 82, 23, 97, 100, 95] # input list to be filtered
2
3# your code here

As an extra thing to try, you can use the map() built-in function to do this too!

1scores = [65, 82, 23, 97, 100, 95]
2# convert to proportions
3proportions = list(map(lambda x: x/100, scores))
4proportions
[0.65, 0.82, 0.23, 0.97, 1.0, 0.95]
 1def score_to_grade(a_score):
 2    # apply transformation to item
 3    if a_score >= 93:
 4        grade = "A"
 5    elif a_score >= 83:
 6        grade = "B"
 7    elif a_score >= 73:
 8        grade = "C"
 9    elif a_score >= 63:
10        grade = "D"
11    else:
12        grade = "F"
13    return grade
14
15scores = [65, 82, 23, 97, 100, 95]
16# convert to letter grades
17grades = list(map(score_to_grade, scores))
18grades
['D', 'C', 'F', 'A', 'A', 'A']

Coordinated iteration across multiple sequences#

One of problems for Project 2 relies on a design pattern I haven’t yet explicitly shown you in clear terms. So I want to quickly review it.

How do you go through the elements of a list, index by index? I’ll show you a form of this, and you can figure out how this might generalize to the rock paper scissors problem, where you need to go through two lists in lockstep (first item from both lists, then second item from both lists, and so on)

In our for loops above that iterated through items in a list, we typically had an iteration variable that directly stored an item from the list at each step.

But we can also define an iteration variable that iterates through a list of indices (remember what indices are in a list? they’re positions in the list!). This will allow us to then use Indexing to grab an item from that index position from our target list.

Here’s an example:

1scores = [65, 82, 23, 97, 100, 95]
2
3# iterate through a list of indices that is of the same length as the `scores` list
4# by convention, people usually name the iteration variable `i`
5for i in range(len(scores)):
6    # and print the score at that index
7    print(scores[i])
65
82
23
97
100
95

And another one:

 1# basic iteration through a list using indices
 2names = ["Joel", "John", "Lane", "Jamie", "Freddy"]
 3
 4# make a list of numbers that start at 0, and stop before
 5# the length of the names list
 6# and go through every number in that list
 7for index in range(len(names)):
 8    # use the number as an index for the names list
 9    name = names[index]
10    # do something with the item at that index
11    print(name)
Joel
John
Lane
Jamie
Freddy

We can extend this pattern to iterate through multiple lists at the same time in a coordinated way. This works as long as the lists are all of the same length: we’ll only need to define a list of indices that are the same length as one of the lists and avoid running into an IndexError (e.g., trying to grab an item from an index position that doesn’t exist from one of the lists that is shorter than the others!)

Here’s an example:

 1# basic iteration through a list using indices
 2names = ["Joel", "John", "Lane", "Jamie", "Freddy"]
 3eligibilities = [True, False, True, True, False]
 4
 5# make a list of numbers that start at 0, and stop before
 6# the length of the names list
 7# and go through every number in that list
 8for index in range(len(names)):
 9    # use the number as an index for the first list
10    name = names[index]
11    # use the same number as an index for the second list
12    eligible = eligibilities[index]
13    # do something with the items at the same index position for both lists
14    print(name, eligible)
Joel True
John False
Lane True
Jamie True
Freddy False

The generic pattern is something like this:

1# make a list of numbers that start at 0, and stop before
2# the length of one of the lists (assuming they are the same length!)
3# and go through every number in that list
4    # use the number as an index for the first list
5    # use the same number as an index for the second list
6    # do something with the items at the same index position for both lists

Indefinite loops (while loops)#

Sometimes you want to repeat actions, but you don’t know in advance how many times you want to repeat. But you do have a stopping condition. In this situation, you can use indefinite loops, which are called so because they keep going until a logical condition becomes False.

Examples:

  • Keep going until I tell you to stop

  • Keep stirring until the sauce thickens

  • Keep taking candy from the box until your bucket is full or the box is empty

Use indefinite/while when you don’t know in advance how many times you want to do something, but do have a stopping condition you can clearly express.

Anatomy of an indefinite (while) loop in Python#

  • The stopping condition defines when the loop will stop and go to the next block of code

    • It’s composed of a Boolean expression

    • It should be possible for the Boolean expression to be False!

  • The block (body) of code is executed once for each iteration in the loop

  • Stopping condition update: It is essential that the body of the loop has some operation it that modifies what is checked in the stopping condition

 1# keep taking steps until you hit a limit
 2steps = 0
 3limit = 10
 4# check stopping condition
 5while steps < limit: 
 6    # body of the loop (aka do some stuff)
 7    print("Taking a step", steps)
 8    # stopping condition update
 9    steps += 1 # 
10print("Done!")
Taking a step 0
Taking a step 1
Taking a step 2
Taking a step 3
Taking a step 4
Taking a step 5
Taking a step 6
Taking a step 7
Taking a step 8
Taking a step 9
Done!
1steps = 0
2limit = 20
3for i in range(limit):
4    # body of the loop (aka do some stuff)
5    print("Taking a step", steps)
6    # stopping condition update
7    steps += 1 # 
Taking a step 0
Taking a step 1
Taking a step 2
Taking a step 3
Taking a step 4
Taking a step 5
Taking a step 6
Taking a step 7
Taking a step 8
Taking a step 9
Taking a step 10
Taking a step 11
Taking a step 12
Taking a step 13
Taking a step 14
Taking a step 15
Taking a step 16
Taking a step 17
Taking a step 18
Taking a step 19

Generic pattern:

1# check stopping condition
2    # body of the loop (aka do some stuff)
3    # stopping condition update
 1guess = input("Try to guess the number between 1 and 10, or say `exit` to quit")
 2number = 5
 3found = False
 4
 5while guess != "exit" and not found:
 6    if int(guess) == number:
 7        print("You got it!")
 8        found = True
 9    else:
10        guess = input("Try to guess the number between 1 and 10, or say `exit` to quit")
Try to guess the number between 1 and 10, or say `exit` to quit 3
Try to guess the number between 1 and 10, or say `exit` to quit 2
Try to guess the number between 1 and 10, or say `exit` to quit exit

Again, it’s helpful to copy-paste one of these programs into python tutor to get an intuition for what is going on.

Some applications of indefinite loops#

Generically: keep doing something until…#

Keep adding characters to a string until it is a defined length, e.g., 10

 1input_string = "abc"
 2
 3# check stopping condition
 4while len(input_string) < 10:
 5    # body of the loop (aka do some stuff)
 6    input_string = input_string + "a"
 7    print(input_string)
 8    # stopping condition update (not needed because we're modifying the thing being checked)
 9    
10print(input_string)
11print(len(input_string))
abca
abcaa
abcaaa
abcaaaa
abcaaaaa
abcaaaaaa
abcaaaaaaa
abcaaaaaaa
10

Keep adding “.” to the string until it is 13 characters long.

1input_string = "abc"
2
3# check stopping condition
4    # body of the loop (aka do some stuff)
5    # stopping condition update
6    
7print(input_string)

PRACTICE: Keep dividing a number by 2 until we can’t anymore.

1num = 12000
2
3# your code here

PRACTICE: Go through a list of people until we have 2 people named “John”.

1names = ["Joel", "John", "Jane", "Jamie", "John", "Michaela", "Sarah", "John", "Chris", "John"]
2
3# your code here

Basic user interfaces (keep running program until user stops us.#

Guessing game.

1guess = input("Try to guess the number between 1 and 10, or say `exit` to quit")
2number = 5
3while guess != "exit":
4    if int(guess) == number:
5        print("You got it!")
6        break # we're done, exit the loop
7    else:
8        guess = input("Try to guess the number between 1 and 10, or say `exit` to quit")
9print("Thanks for playing!")
1# check stopping condition
2    # body of the loop (aka do some stuff)
3    # stopping condition update

All of the definite loops we saw earlier can be implemented with indefinite loops!#

 1# input list
 2scores = [65, 82, 23, 97, 100, 95]
 3
 4# output list
 5proportions = []
 6
 7# initialize index variable
 8i = 0
 9# check stopping condition: 
10# i is less than the length of the list?
11while i < len(scores):
12    # body of the loop (aka do some stuff)
13    # apply the transformation
14    proportion = scores[i]/100
15    # add the transformed value to the output list
16    proportions.append(proportion)
17    # stopping condition update
18    i += 1
19
20proportions
[0.65, 0.82, 0.23, 0.97, 1.0, 0.95]

Breaking a loop with the break statement#

The break statement ends the current loop and jumps to the statement immediately following the loop. It is like a loop test that can happen anywhere in the body of the loop

 1found = False # default is we hvaen't found it
 2names = ["Joel", "John", "Jane", "Jamie", "Lisa", "Anna", "Fred"]
 3for name in names:
 4    print(name)
 5    if name == "John":
 6        found = True # set found to true
 7        print("Found!")
 8        break
 9print("We're done with the loop")
10if found:
11    print("Found john!")
12else:
13    print("Didn't find john")
 1found = False # default is we hvaen't found it
 2names = ["Joel", "John", "Jane", "Jamie", "Lisa", "Anna", "Fred"]
 3
 4name = names.pop()
 5while not found:
 6    print(name)
 7    if name == "John":
 8        found = True
 9        print(found)
10        break
11    else:
12        if len(names) > 0:
13            name = names.pop()
14
15if found:
16    print("Found john!")
17else:
18    print("Didn't find john")
1while True:
2    line = input('> ')
3    if line == 'done' :
4        break
5    print(line)
6print('Done!')

Common errors#

Indentation is key!#

The way that Python knows what counts as the body of code for a loop (whether definite or indefinite) is through indentation.

You must indent all code that goes in the body underneath the for/while statement (after the colon).

If you fail to indent the first line of code in the body, you will get an IndentationError.

If you fail to indent anything after the first line of code in the body, you will be committing a semantic error: Python will not alert you because it is legal code. But your program will not do what you intend it to do.

1for i in range(5):
2print(i)
  File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_34482/3695896917.py", line 2
    print(i)
    ^
IndentationError: expected an indented block
1# i want to step through a list of numbers, multiply each of them by 5 and print htem out
2nums = [1,2,3,4,5]
3for num in nums:
4  new_num = num*5
5print(new_num)
25

IndexError when looping through a list#

This comes up mostly with while loops. So, while it’s possible to do any for loop with a while loop, you want to be careful with it.

 1#
 2#
 3#
 4names = ["Joel", "John", "Jane", "Jamie", "John"]
 5to_grab = [] # output list, initialize to empty list
 6
 7index = 0 # set initial index to zero
 8while index < 10: # until you reach the end of the list
 9  print(index)
10  name = names[index] # get the name at this index
11  if name == "John": # check if is john / meets my criteria for being filtered
12    to_grab.append(name) # add the item to the output list
13  index += 1 # increment the index
14
15# print out the result
16print(to_grab)
0
1
2
3
4
5
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_34482/4102397174.py in <module>
      8 while index < 10: # until you reach the end of the list
      9   print(index)
---> 10   name = names[index] # get the name at this index
     11   if name == "John": # check if is john / meets my criteria for being filtered
     12     to_grab.append(name) # add the item to the output list

IndexError: list index out of range
1# basic iteration through a list using indices
2names = ["Joel", "John", "Lane", "Jamie", "Freddy"]
3
4for index in range(6):
5  name = names[index]
6  print(index, name)
0 Joel
1 John
2 Lane
3 Jamie
4 Freddy
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_34482/4256380183.py in <module>
      3 
      4 for index in range(6):
----> 5   name = names[index]
      6   print(index, name)

IndexError: list index out of range

Infinite loops#

Remember that with indefinite loops, we need the stopping condition to be False at some point. Or at least, give the loop a way to exit / break. Otherwise, it will go forever! A common error is to forget to include any block of code in the body (block) of the loop that modifies the stopping condition or provides a break condition.

1# 
2n = 5
3while n > 0:
4    print(n)
5    n = n - 1
6print("Blast off!")
5
4
3
2
1
Blast off!
1# 
2n = 5
3while n > 0:
4  print(n)
5  n = n-1
6print("Blast off!")
 1#
 2#
 3#
 4names = ["Joel", "Jane", "Jamie"]
 5to_grab = [] # output list, initialize to empty list
 6
 7index = 0 # set initial index to zero
 8while len(to_grab) == 0: # until you reach the end of the list
 9    print(index)
10    name = names[index] # get the name at this index
11    if name == "John": # check if is john / meets my criteria for being filtered
12        to_grab.append(name) # add the item to the output list
13    index += 1 # increment the index
14
15# print out the result
16print(to_grab)