Looping list students_period_A to combine students_period_B

Codecademy Question:
“Suppose we have two lists of students, students_period_A and students_period_B . We want to combine all students into students_period_B .”

Provided Code Solution (doesn’t work)
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)

Result from Provided Code Solution
Alex
Briana
Cheri
Daniele

It doesn’t print the names from list B, which is what the prompt is asking for.

I have looked over every discussion post I can find and I still cannot figure out how to solve this. Please assist.

Hi :slight_smile: !

Right now your code is iterating items list A and adding them to list B. You’re also printing every student from list A.

Is the prompt to print or return? Effectively, your B list right now does combine both original lists. You just have to figure out what you want to do with that end result.

Formatting is always important in python but in general also it’s easier for people to help when the code is formatted, so for future reference: [How to] Format code in posts

1 Like

Ok. That was very helpful. I didn’t realize the proper formatting to post code.

This would be correct, right?

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 am learning to be more specific. Always a good trait. Yes, I want to print to screen the combined A and B student lists. I cannot figure out how. Thanks in advance.

Well, it doesn’t hurt to experiment!

With the for-loop, you’ve supposedly affected list B.
What happens when you try to print list B outside of the context of the for-loop? What about A?

This kind of poking around is sometimes useful, particularly in debugging and in getting to know unfamiliar code.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.