3.9. While Loops With Lists#
It’s often useful to use a loop to iterate through a list. Suppose you had the following list of colours. You can print out each colour using the following code:
colours = ["red", "yellow", "pink", "green", "purple", "orange", "blue"]
print(colours[0])
print(colours[1])
print(colours[2])
print(colours[3])
print(colours[4])
print(colours[5])
print(colours[6])
What you’ll have noticed is that the code is quite repetitive. If you ever see code that is repetitive, it often means it can be replaced by a loop! Have a look at the example below.
Recall that ``len()`` tells you the number of elements in the given list. In this case is ``len(colours)`` is 7.
colours = ["red", "yellow", "pink", "green", "purple", "orange", "blue"]
i = 0
while i < len(colours):
print(colours[i])
i = i + 1
Question 1
What do you think the output of the following will be?
numbers = ['one', 'two', 'three', 'four', 'five']
i = len(numbers) - 1
while i >= 0:
print(numbers[i])
i = i - 1
Solution
Let’s trace through this code.
len(numbers) gives us the number of elements in the list numbers, which in this case is 5. This means that initially i is 4 since i = len(numbers - 1).
We can also see that the condition we have set for our while loop is i > 0.
At the first iteration i = 4. This means the condition is True. We execute the code block:
print(numbers[i])
i = i - 1
This means that we output numbers[4]:
five
At the second iteration i = 3. This means the condition is True. Again, we execute the code block and we output numbers[3]:
four
At the third iteration i = 2. This means that the condition is True. Again, we execute the code block and we output numbers[2]:
three
At the fourth iteration i = 1. This means that the condition is True. Again, we execute the code block and we output numbers[1]:
two
At the fifth iteration i = 0. This means that the condition is True. Again, we execute the code block and we output numbers[0]:
one
At the sixth iteration i = -1. This means that the condition is False. We do not execute the code block.
Question 2
What do you think the output of the following will be?
numbers = ['one', 'two', 'three', 'four', 'five']
new_list = []
i = len(numbers) - 1
while i >= 0:
new_list.append(numbers[i])
i = i - 1
print(new_list)
['five', 'four', 'three', 'two', 'one']
['four', 'three', 'two', 'one']
['one', 'two', 'three', 'four', 'five']
['two', 'three', 'four', 'five']
Solution
Solution is locked
Question 3
What do you think the output of the following will be?
fruits = ['apple', 'banana', 'cherry', 'date', 'eggplant', 'fig', 'grape']
i = 0
while i <= len(fruits):
print(fruits[i])
i = i + 2
Solution
Solution is locked
Question 4
What do you think the output of the following will be?
numbers = [5, 3, 8, -3, 0, 2]
total = 0
i = 0
while i < len(numbers):
total = total + numbers[i]
i = i + 1
print(total)
Solution
Solution is locked
Question 5
Rewrite the following code using a while loop.
symbols = ['@', '%', '#', '+', '^']
print(symbols[0])
print(symbols[0] + symbols[0] + symbols[0])
print(symbols[1])
print(symbols[1] + symbols[1] + symbols[1])
print(symbols[2])
print(symbols[2] + symbols[2] + symbols[2])
print(symbols[3])
print(symbols[3] + symbols[3] + symbols[3])
print(symbols[4])
print(symbols[4] + symbols[4] + symbols[4])
Solution
Solution is locked
Code challenge: All the numbers!
You have been provided with a list of numbers.
numbers = [58, 67, 48, 12, 67, 88, 50, 54, 13, 46, 89, 98, 27, 13, 83]
Write a program that prints out each of these numbers on a new line and at the end says That’s all the numbers!
The output of your program should look like this:
58
67
48
...
13
83
That's all the numbers!
Note
The … indicates that there are more numbers that are just not shown in the example. Here we only show the first 3 lines and last 3 lines of output.
Solution
Solution is locked