FAQ: Learn Python: Loops - Infinite Loops

Sorry, I have two questions - this one is about the exercise itself.

While the suggested solution code achieves what is asked, to me it prints the incorrect value:

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)

As student equals the value of the current iteration number, and it prints this every time the loop is run, what is printed is simply the list of values in students_period_A.

Would it be more correct to write the following?

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)

Now we only print the output of the variable we’re adding to after the loop is done iterating each appended value.

I know this is a tiny difference but it helps to check my intuitions here to see if this is a minor mistake in the format or if I’m misunderstanding things!

Yes, the latter version will give a better picture of the overall result.

The print statement within the loop is just printing each student in students_period_A. A couple of print statements after the loop allow us to see what the two lists look like after the loop has finished.

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

print(students_period_A)  
# students_period_A is unchanged
print(students_period_B)
# students_period_B is mutated/modified with four new elements
list_A = [1, 2, 3, 4, 5]

If we wanted to use a loop to double each element and replace the existing element, then a
for element in list_A:     loop won’t allow us to accomplish the goal,

for element in list_A:
    element = element * 2

print(list_A)
# [1, 2, 3, 4, 5]

But, we can use a for loop to iterate over the indices so that we can update the elements at those indices,

for i in range(len(list_A)):
    list_A[i] = list_A[i] * 2

print(list_A)
# [2, 4, 6, 8, 10]
1 Like

This thread is wild and insanely confusing for someone new to all of this LOL

The instructions clearly state “…fix it to accomplish the original goal of combining all students…” however the solution under “Get Unstuck” is not this at all as it just prints the first list which is not what the instructions have asked you to do

This is the answer:

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

hi, here is how i solved this 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(student)

for student_b in students_period_B:
students_period_A.append(student_b)

print(student_b)

This is my answer using loops to solve the problem:

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

combined_students =

for student in students_period_A:
combined_students.append(student)

for student in students_period_B:
combined_students.append(student)

print(combined_students)

1 Like

I’ll share something interesting that I’ve learned at the process of fixing the loop on this exercise:

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)

Outputs:

["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"]
  • That happens because the “print(students_period_B)” action is inside of the loop;
  • Since the “.append()” is a method that can only add to the end of a list a single item at a time, we get to see this process happening through the “print(students_period_B)” action;
  • “student” is assigned individually to each value of the “students_period_A” list, these values are then appended to the “students_period_B” list, just like we wanted.

On the other hand, when we write the code like this:

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)
  • “print(students_period_B)” is not a part of the loop anymore, so we don’t get to see all of the “appends” happening one by one, so our output looks like this:
["Dora", "Minerva", "Alexa", "Obie", "Alex", "Briana", "Cheri", "Daniele"]

That’s just something that I found interesting, the difference of placing the “print” action inside or outside of the loop, as well as learning once again how the “.append()” method really works.

1 Like

I have some troubles with it. Its print the first name of student_A four times…