FAQ: Loops - While Loops: Lists

This community-built FAQ covers the “While Loops: Lists” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Scientist
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise While Loops: Lists

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi, I don’t know if this belongs here, but I noticed that on this exercise codecademy accepted this line:

while index < length:

but didn’t accept this:

while length > index:

It threw me off for a minute there, because both output the same answer. :upside_down_face:


I need help with the solution below.

python_topics = [“variables”, “control flow”, “loops”, “modules”, “classes”]

#Your code below:
length = len(python_topics)
index = 0
while index < length:
print("I am learning about " + python_topics[index])
index += 1

Why does the loop output the full list if the length is 4, and once the index reaches 4, then it’s not less than 4, it’s equal to 4? I tried using index <= length, but I got an error that it was out of the index range. Can someone help me understand this better?

In Python, as in many other languages, list indices begin at zero, not 1. When index reaches 5 (the length of the list) it points to an element that does not exist.

      0             1            2         3          4
[“variables”, “control flow”, “loops”, “modules”, “classes”]

As we can see, there is no index 5.


Thank you very much for your help!

1 Like

I don’t understand how this is wrong. Can anyone explain, or is this an error? I get the correct output

1 Like

Dear Community,

my question would revolve around the fact that in this exercise, we created a variable assigned to the value len(list) as a termination condition for the for-loop. I wondered why we had to create this variable as opposed to directly employing len(list), therefore “sparing” the declaration of the length variable itself?

Written in code, why is the following notation preferred:

for i in length:

as opposed to

for i in len(list):


I was thinking that it would be computationally more inefficient to compute the length of a list upon every iteration, but wondered if this was true or if there was “something else to it”.

Thank you very much for your remarks in advance!

I think it’s more in showing that can more than we should. Logic dictates, but sometimes tossing in a gratuitous variable makes it easier for the reader and we can use less documentation. Let the code speak for itself, as it were.

Could someone help me to understand some things with this exercise please?

  1. Does anyone know why this code isn’t accepted?

length = len(python_topics)

index = 0

while length > index:
for element in python_topics:
print('I am learning about '+element)
index += 1

  1. On the given solution - the list element is printed each iteration using the variable ‘index’ in the square brackets in the loop. I thought the variable named index was declared to serve as the value that increments each loop iteration. How is the index variable connected to the list elements?

Above is all the code you need. The rest is superfluous.

Thank you

Do you know re. the 2nd question - I don’t understand how the term ‘index’ is used in the loop to refer to the index of each element of the list as it iterates through the loop. I thought ‘index’ was a variable declared to be checked in the condition of the loop (against the length of the list) and incremented each iteration. How does the code know when we use our variable ‘index’ that it should also refer to the actual index of the list?

In order to reproduce with while what the for loop is doing, we need to consider the difference between element (a value IN the list) and index, one of list length (by count) indices.

#              ---iterable---
for element in python_topics:
    print ('I am learning about ' + element)
#                                   -value-

The while loop is not iterating the iterable and its values. We must set up the scenario by which it will output the same lines as the for loop.

index = 0
length = len(python_topics)

We’ll use those variables and subscript of the iterable to access the values at each index.

while index < length:
    print ('I am learning about ' + python_topics[index])
    index += 1

See how that works?

Bottom line, one form or the the other, but not both at the same time.


does anyone know why this code keeps returning an indefinite loop? thank you in advance.

Line 8 bears consideration.


Do you think the problem is line 8 index +=1 ?


The problem was the last line 8 was not indented to line up with the line above it once I corrected that it printed.

1 Like

Oops. Accidentally hit enter. Just doing a little experimenting with while loops. My question is, when I change the bottom index to 2, (index += 2), why does it only take away “I am learning about modules.” statement as opposed to showing all of them when index += 1?

Hi, can anyone explain why you have to put [index] in this? the explanation on code academy doesn’t make sense to me.


I wasn’t sure where to ask this but you seem to know what you’re talking about. How does the system know that index (in lists/while loops) is corresponding to the index of a list? Is index a function or a variable? I don’t understand lol, thank you sir!

index in this instance is a variable. What we call it is inconsequential, but makes sense to name it for what it represents. The system doesn’t ‘know’ anything that we don’t tell it, explicitly. It will know an object is a list by examining the data structure, which will have type 'list`. Lists are subscriptable which is denoted by the square brackets.

my_list = [1,2,3,4,5,6,7,8,9]
index = 4
print (my_list[index])    #  5
#                \
#             subscript
1 Like