 # FAQ: Learn Python: Loops - Infinite Loops

yup - starting to make sense now - thanks ever so much. I’m just afraid of what other important bit of info fell out of my brain to accommodate this one 1 Like

Hi all.

I´m very new to all of this, so please excuse me if I fail to use the correct term /jargon for things.

Anyway, I managed to get to the end of the exercise and compare it to the default answer. Turns out I got it right, but I don´t understand one thing.

The final print out of the exercise printed only the items from students_period_A, even though throughout the exercise they reference combining both lists. Shouldn´t the final print out be a list of both lists combined? I feel like all I did was replace the items in students_period_B with the items in list students_period_A.

students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”]
students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”]
for student in students_period_A:
students_period_B.append(student)
print(student)

Kind regards,
Leandro Johann Silva

3 Likes

I have the same train of thought and question. 1 Like

Apparently we´re not getting our responses. Did you manage to figure it out?

1 Like

Consider what your are looping though here (`students_period_A`) and what values `student` would take on each iteration of this loop-

``````for student in students_period_A:
students_period_B.append(student)  # we alter the list: students_period_B
print(student)  # this prints once every iteration
``````
1 Like

I have the same question and don’t really understand the response.

If we are looping each element in students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”] , and/or taking each element from students_period_A and adding it to the end of students_period_B I would of expected the code to print the list of names four times adding one name from students_period_A each time?

why does it only print the names in students_period_A ?
Dora
Minerva
Alexa
Obie

1 Like

The question asked in the post I had responded to which you are now replying to seems different from what you are asking. Could you provide the code your question pertains to?

Hi, i have the same question as @gpol. the lesson 4 infinite loops in Loops asking us to combine student list A and B into one using append. I have done the coding as attached. Suppose the output of the coding should include both list, but i only able to get the student list in student list A, not combine with B (check the picture for the output)

. why?

[code](https://students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”] students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”] for student in students_period_A: #students_period_A.append(student) students_period_B.append(student) print(student))

1 Like
``````for student in students_period_A:
students_period_B.append(student)
print(student)
``````

In your loop, you are iterating over each element in `students_period_A`. After appending it to the end of `students_period_B`, you are printing out the value of `student`.

Each iteration, `student` is assigned the next value in the list `students_period_A`. This means, `student` will have the value of `"Alex"` the first iteration, `"Briana"` the second iteration, `"Cheri"` the third iteration, and so on.

The statement `print(student)` prints the value of `student` during that iteration. This means that `"Alex"` is printed the first iteration, `"Briana"` the second iteration, and so on.

You mentioned that the output should “include both lists,” what do you mean by this? The instructions state to print only `student` during each iteration, which is what your code accomplishes.

2 Likes

Oh! I just realized that that is only “student” in the print statement. sorry, this is my mistake.
What i said to print to include both list shall be printing the student list B, not the student.
Thank you for your kind response! It solved my problem!

This lesson had me scratching my head, because i can’t understand why it would repeatedly append itself.

``````my_favorite_numbers = [4, 8, 15, 16, 42]

for number in my_favorite_numbers:
my_favorite_numbers.append(1)
``````

i would expect the new result to be [4, 8, 15, 16, 42, 1 ], but why does it have to append itself again infinitely instead of printing out the result?
I can’t find the answer anywhere because they’re all talking about ‘while loops’ everywhere else.

I found this lesson unsatisfactory, as far as explanations go.

The issue is that you’re iterating through a list- `my_favorite_numbers` and constantly appending to that same list within the loop. So your list is constantly growing with each new append so your loop never reaches the end of the list.

1 Like

for student in students_period_A:
students_period_B.append(student)
print(students_period_B)

Here my code (without the given variables). It runs with the variables added, iterating 4 lists with a new student each time. Does anyone know how to get it to only print the last iteration since that should be the final answer?

I was able to figure it out with the next lesson. Here’s an updated code if anyone had the same question:

for student in students_period_A:
students_period_B.append(student)
if student == students_period_A:
print(students_period_B)

In this exercise they use the following loop:

``````students_period_A = ["Alex", "Briana", "Cheri", "Daniele"]
students_period_B = ["Dora", "Minerva", "Alexa", "Obie"]

for student in students_period_A:
students_period_B.append(student)
print(student)
``````

My question is, why does this for loop does not have something like

``````student += 1
``````

?

Short answer, because it doesn’t need one. Python’s `for` loops act like for-each or similar loops in other languages, it will iterate through each element of iterable object you pass to it, no index needed.

This saves you having to add extra variables, sum increments and start/stop at particular indices but perhaps more importantly is that it’s just simpler syntax and therefore easier to read, understand and edit in the future-

``````for x in range(3):
print(x)

Out: 0
1
2
``````

Try printing the output of `student` on each iteration of your example if you wanted a clear view of what it’s doing.

If it’s not clear then perhaps a view of the docs would help- https://docs.python.org/3/tutorial/controlflow.html#for-statements

2 Likes