# 7.5. Lists and for loops¶

It is also possible to perform list traversal using iteration by item. A list is a sequence of items, so the for loop iterates over each item in the list automatically.

It almost reads like natural language: For (every) fruit in (the list of) fruits, print (the name of the) fruit.

Remember the turtle drawings we made earlier? For loops and lists can be applied there as well!

## 7.5.1. Iteration Simplifies our Turtle Program¶

To draw a square we’d like to do the same thing four times — move the turtle forward some distance and turn 90 degrees. We previously used 8 lines of Python code to have alex draw the four sides of a square. This next program does exactly the same thing but, with the help of the for statement, uses just three lines (not including the setup code). Remember that the for statement will repeat the forward and left four times, one time for each value in the list.

While “saving some lines of code” might be convenient, it is not the big deal here. What is much more important is that we’ve found a “repeating pattern” of statements, and we reorganized our program to repeat the pattern.

The values [0,1,2,3] were provided to make the loop body execute 4 times. We could have used any four values. For example, consider the following program.

In the previous example, there were four integers in the list. This time there are four strings. Since there are four items in the list, the iteration will still occur four times. aColor will take on each color in the list. We can even take this one step further and use the value of aColor as part of the computation.

In this case, the value of aColor is used to change the color attribute of alex. Each iteration causes aColor to change to the next value in the list.

The for-loop is our first example of a compound statement. Syntactically a compound statement is a statement. The level of indentation of a (whole) compound statement is the indentation of its heading. In the example above there are five statements with the same indentation, executed sequentially: the import, 2 assignments, the whole for-loop, and wn.exitonclick(). The for-loop compound statement is executed completely before going on to the next sequential statement, wn.exitonclick().

iter-5-1: How many times will the for loop iterate in the following statements?

p = [3, 4, "Me", 3, [], "Why", 0, "Tell", 9.3]
for ch in p:
print(ch)

• 8
• Iteration by item will process once for each item in the sequence, even the empty list.
• 9
• Yes, there are nine elements in the list so the for loop will iterate nine times.
• 15
• Iteration by item will process once for each item in the sequence. Each string is viewed as a single item, even if you are able to iterate over a string itself.
• Error, the for statement needs to use the range function.
• The for statement can iterate over a sequence item by item.
iter-5-2: How does python know what statements are contained in the loop body?
• They are indented to the same degree from the loop header.
• The loop body can have any number of lines, all indented from the loop header.
• There is always exactly one line in the loop body.
• The loop body may have more than one line.
• The loop body ends with a semi-colon (;) which is not shown in the code above.
• Python does not need semi-colons in its syntax, but relies mainly on indentation.

iter-5-3: Consider the following code:

for aColor in ["yellow", "red", "green", "blue"]:
alex.forward(50)
alex.left(90)


What does each iteration through the loop do?

• Draw a square using the same color for each side.
• The question is not asking you to describe the outcome of the entire loop, the question is asking you about the outcome of a **single iteration** of the loop.
• Draw a square using a different color for each side.
• Notice that aColor is never actually used inside the loop.
• Draw one side of a square.
• The body of the loop only draws one side of the square. It will be repeated once for each item in the list. However, the color of the turtle never changes.
Next Section - 7.6. The Accumulator Pattern