Unable to add one list to another list when using a for loop

I am unable to add a list to another list using a infinite loop (for loop). Here is the problem:

Suppose we have two lists of students, students_period_A and students_period_B . We want to combine all students into students_period_B . Write a for loop that goes through each student in students_period_A and adds it to the end of students_period_B .

Here is my code:

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

for students_period_B in students_period_A:
  students_period_A.append(students_period_B)

How would does it know when this list ends?

This section is talking about infinite loops which is why I think I need to use it:

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/learn-python-loops/exercises/infinite-loops

Hi @jrb0831!

A couple of things:

  • infinite loops are what you want to avoid! They can often crash the program and have to be exited manually. It’s more common to run into them when you have while-loops (but that’s another topic).

  • a for-loop is a very common and powerful tool in programming. Don’t let its ubiquity fool you, it is very handy and often has surprising ways of being useful.

  • a for-loop can be translated into plain English with a sentence like: for each item in your list, do this action.


In code that would look like this:

for item in list:
    action

Note, that the only 2 hard requisites are that the list be an iterable object (which basically means you can cycle through it. I think of it like numbered key lockers), and that the actions have to be valid.

What is flexible is the naming of the item — it’s just a variable place holder. Often you will see the letter i pop up as a popular variable:

for i in list:
   action

So now for a real example:

list_1 = ["pita", "bread"]
list_2 = ["is", "a", "viable", "food"]

for item in list_2:
   list_1.append(item)

print(list_1)
#output
#["pita", "bread", "is", "a", "viable", "food"]

In plain English, this code’s commands are to:

For every item in the second list, add that item to the first list. (By default it will go in order, later there are ways to make it go in reverse and all sorts of zany stuff, but basics are important first).

3 Likes

Infinite loops should always be avoided. This lesson is not asking you to create an infinite loop.

This step instructs you to add each student in students_period_A to the end of students_period_B.

You should use another variable name (not students_period_B) here to avoid confusion and a duplicate variable name.

Make sure you’re appending each student to the appropriate list.

2 Likes

I’m very new to coding, so make sure to verify this is correct with someone who knows more than I do. That being said, I believe the reason it will know to stop is simply because the list it looks into stops. Being that you are telling the loop to look into “students_period_B” it should stop when it gets to the end of that list.
Now, as for if that code works or not, it’s not exactly how I wrote it. But again, I’m new to this, so there’s no guarantee that the way I did it was the best way.
Loops are tough. I’m still working on them too.

3 Likes

Hey @toastedpitabread

Your explanations rock! They are also digestible and easy to understand.

Anyways thanks and I enjoyed your code. Here is my opinion.

 list_1 = ["pita", "bread"]
list_2 = ["goes", "good","with","hummus"]

for item in list_2:
   list_1.append(item)

print(list_1)
#output
#["pita", "bread", "is", "a", "viable", "food"]
1 Like

Thank you appreciate the help!

1 Like

Thank you very much!