while loop isn’t only good for counting! Similar to how we saw
for loops working with lists, we can use
while loops to iterate through a list as well.
Let’s return to our ingredient list:
ingredients = ["milk", "sugar", "vanilla extract", "dough", "chocolate"]
We know that
while loops require some form of a variable to track the condition of the loop to start and stop.
Take some time to think about what we would use to track whether we need to start/stop the loop if we want to iterate through
ingredients and print every element.
Click here to find out!
We know that a list has a predetermined length. If we use the length of the list as the basis for how long our
whileloop needs to run, we can iterate the exact length of the list.
We can use the built-in Python
len() function to accomplish this:
# Length would be equal to 5 length = len(ingredients)
We can then use this
length in addition to another variable to construct the
length = len(ingredients) index = 0 while index < length: print(ingredients[index]) index += 1
Let’s break this down:
# Length will be 5 in this case length = len(ingredients)
As mentioned, we need a way to know how many times we need our loop to iterate based on the size of the collection.
This comes in the form of our
lengthvariable which stores the value of the length of the list.
# Index starts at zero index = 0
We still need an additional variable that will be used to compare against our
while index < length:
whileloop conditional, we will compare the
indexvariable to the length of our list stored inside of the
On the first iteration, we will be comparing the equivalent of
0 < 5 which will evaluate to
True, and start the execution of our loop body.
# The first iteration will print ingredients print(ingredients[index])
Inside of our loop body, we can use the
indexvariable to access our
ingredientslist and print the value at the current iteration.
indexstarts at zero, our first iteration will print the value of the element at the zeroth index of our
ingredientslist, then the next iteration will print the value of the element at the first index, and so on.
# Increment index to access the next element in ingredients # Each iteration gets closer to making the conditional no longer true index += 1
On each iteration of our
whileloop, we need to also increment the value of
indexto make sure our loop can stop once the
indexvalue is no longer smaller than the
This increment also helps us access the next value of the
ingredientslist on the next iteration.
Our final output would be:
milk sugar vanilla extract dough chocolate
Let’s use a
while loop to iterate through some lists!
We are going to write a
while loop to iterate over the provided list
First, we will need a variable to represent the length of the list. This will help us know how many times our
while loop needs to iterate.
Create a variable
length and set its value to be the length of the list of
Next, we will require a variable to compare to our length variable to make sure we are able to implement a condition that eventually allows the loop to stop.
Create a variable called
index and initialize the value to be
Let’s now build our loop. We want our loop to iterate over the list of
python_topics and on each iteration print
"I am learning about <element from python_topics>". For this loop we will need the following components:
- A condition for our
- A statement in the loop body to print from our condition
- A statement in the loop body to increment our index forward.
The end result should output:
I am learning about variables I am learning about control flow I am learning about loops I am learning about modules I am learning about classes
If you notice the Run button spinning continuously or a “Lost connection to Codecademy” message in an exercise, you may have an infinite loop! If the stop condition for our loop is never met, we will create an infinite loop which stops our program from running anything else. To exit out of an infinite loop in an exercise, refresh the page — then fix the code for your loop.