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.

2 Likes

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:

length=len(list)
for i in length:
   <code>

as opposed to

for i in len(list):
   <code>

?

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.