FAQ: Learn Python: Loops - Infinite Loops

for student in range(1):

student = str(students_period_A + students_period_B)

print(student)

I wrote that and it worked.

1 Like

Hope the solution below is of some use …

Define lists of students for each period

students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”]
students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”]

Combine lists using for loop

for student in students_period_A:
students_period_B.append(student)

Uncomment print statement to watch students_period_B list grow

#print(students_period_B)

Print list to confirm result

print("\n")
print(students_period_B)

Hi, I’m having trouble with the “fixing the infinite loop” lesson. I believe I understand what the page is asking me to do, but I don’t understand how to give it the right answer. Which answer, if any, is correct, below?

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(students_period_B)

This code works for me, but I don’t feel like it’s the right way to do it. Am I wrong?

This code also works for me.

for student in students_period_A + students_period_B:
print(student)

1 Like

This whole thread is bizarre. What people want is an answer to this simple question:

“How can I make a loop that combines the two lists that will print this answer:”

Alex
Briana
Cheri
Daniele
Dora
Minerva
Alexa
Obie

I’ve seen:

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

Which prints:

Alex
Briana
Cheri
Daniele

This is, of course, not the answer. And every other example is also, simply put, not right.

After some digging on stack overflow, I come to the real answer. Append is the culprit, and must be destroyed. Then, use this:

for student_A in students_period_A:
  print(student)

for student_B in students_period_B:
  print(student_B)

You will have the list you want after all :slight_smile:

(Or the following for the “correct” answer, even though it doesn’t print what you think it should do):

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

I think it’s a case of trying to perform two different tasks at the same time. One to modify students_period_B and one to print it out. The instruction on the left mention the following-

Suppose we have two lists of students, students_period_A and students_period_B . We want to combine all students into students_period_B .

Under most circumstances I’d assume that meant to directly modify the students_period_B list. So loop & .append, + and re-assign, .extend or += would all do this job.

But trying to print whilst that’s being done seems like a mistake and causes more confusion. It’s much simpler to modify the list and then print.

# for the append example...
for student in students_period_A:
    students_period_B.append(student)

print(students_period_B)
1 Like

if student isn’t initialized to zero and not updated how does it know to cycle through the list?

It’s relying on the fact the list object is iterable. Unlike arrays in some languages Python’s lists are objects (wrapping an underlying dynamic array but the fact they’re instances of a particular type is the important part) and they come with certain methods with added behaviour. One of those is the option to iterate element by element (an implementation of the OOP iterator pattern).

So instead of directly accessing elements by index you can rely on the implementation of a list to return contained elements on by one in a for loop.

I made a recent reply about for loops that might help but what you really want to look into if you’re used to a language where indexing is a necessity is iterables and iterators in Python. They’re used all over the place and form the basis of Python’s for loops (even when using range)-

With all these explanations I decided to mess around and write what I learned to this forum. Hopefully this helps someone in the future.
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(students_period_B)

Because this is a “for” loop, what we end up with is the program appending a single element from “student” (which is equal to “students_period_A”), to “students_period_B” and printing out the new list every single time it does the loop, until list “students_period_A” has no more elements to append to list “students_period_B”.

Our output would be:
[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]


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

Exercising this code will add list “students_period_B” to list “students_period_A” for every element in list “students_period_A” so we will get 4 identical outputs:

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]


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

This code will add list “students_period_B” to list “students_period_A” as a single loop since our range() is only 1.
Our output would be:

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

1 Like

students_period_A = [“Alex”, “Briana”, “Cheri”, “Daniele”]
students_period_B = [“Dora”, “Minerva”, “Alexa”, “Obie”]

students_period_B = students_period_A + students_period_B

for student in students_period_B:

print(student)

I don’t understand anything right here, I try to combine students from A to B, but it’s not working, here’s what I typed:


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)

I don’t understand why the B list isn’t getting each students from list A.

If I try to decompose each line it should be :

for x student in A list
B list get this x student
(all students names should be printed)

I don’t understand why it’s just printing student from list A

Hey, welcome to the forums! :wave:

At the moment you are iterating through every student in students_period_A, adding that student to list B, then printing the student. At no point in the code are you printing the students stored in students_period_B, only students_period_A as that’s all you’re iterating through.

If you print the value of students_period_B after the loop, you’ll see that they have been combined successfully, and you could iterate through list B after the merging is completed to print everyone:

Happy coding! :slight_smile:

3 Likes

Thank you, I understand now!

1 Like

You have to print the list of students_period_B, comment out the print statement of students_print_A first.

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)
for student in students_period_B:
  print(student)

Thank you to everyone for their help and feedback. Reading through the posts have answered a few of my questions but I’m still confused on some of the basics.

  1. What exactly are we iterating here? Are we just continuously outputting Alex, Briana, Cheri and Daniele over and over?
    image
  1. Moreover, whats the difference btw the above and below if we remove the append line? We get the same output so what exactly is the append function doing?
    image

Thank you again to everyone

For something like this I think it’s worth having a quick test as it’s often weirdly helpful to just do it for yourself. It does indeed output those four names in turn but it would only do so once. It basically steps through each one in turn until it reaches the end of the list and then automatically terminates. This is exactly what you see when you remove the .append line (each element in turn, but just once).

I think that example is just a bit awkward really. It’d be better to have one loop join the two lists and a second to iterate through the new combined list. Or, you could just print the new combined list without a loop if you liked, e.g

for student in students_period_A:
    students_period_B.append(student)

print(students_period_B)

Note that you’ve changed the value of students_period_B from then onwards.

Here is my first try to the problem –

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(students_period_B)

This gave me an output of:
[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’]
[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’]
[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’]
[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

For awhile I was wondering what I was doing wrong, and then I decided to take out the indentation of print(students_period_B) which initially gave me the right output of:

[‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

I only indented my first answer because I remembered in the lesson to always indent the ‘for’ loops or else I will get an indentationerror … but here I did not. Wondering why. ty

1 Like

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

Second Answer:

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

I think your first answer does not meet the request. That is simply printing 2 lists back to back. And this is not automated.
The second answer you provided is just the mirror of the answer that is most commonly posted. It modifies period_A instead of modifying period_B as requested in the instructions. And in the end still doesn’t provide the answer your looking for.

I think the correct answer was provided above by genius sevrotheloyal and is mind-blowingly simple:
‘’’
for student in students_period_A + students_period_B:
print(student)
‘’’

print(students_period_A + students_period_B) works, no need to explicitly loop

2 Likes