# FAQ: Learn Python: Loops - Infinite Loops

This works perfectly.

``````for student in students_period_A:
student =  students_period_A + students_period_B
print(student) # Don't indent here
``````

Hi. I am new to programming. I just started a few weeks ago so I am sorry if this question sounds dumb to some of you.
In this exercise, I thought the main purpose was to combine the two lists. Can someone please explain to me how to do that? I have been reading about it but no results. In advance Thanks for the help.

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

for number in my_favorite_numbers:
my_favorite_numbers.append(1)

What can be a solution for this code ? so that it outputs:
[41, 81, 151, 161, 421]

1 Like

`my_favorite_numbers = list(map(lambda y: y*10+1, my_favorite_numbers))`

1 Like

Hi Javier,

The task is asking to combine the two lists (students from period A with students from period B) using a FOR LOOP (the first type of loop we learned in the course).

Here are the lists:

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

So how do we append students from period A to period B? Is very simple. We create a FOR LOOP:

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

``````

In this case, we will only print the variables from this specific function; to print the final list, you need to add a print function that is not indented to this function; as the example below:

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

print(students_period_B)
``````

I’ve seen many wrong examples above of people combining and printing this lists as strings; although that also work, the purpose of this exercise is to use FOR LOOPS and test the indentation ( Indentation refers to the spaces at the beginning of a code line . Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.)

2 Likes

Why isn’t the function method in infinite loops not mentioned? E.g., I want to call out a function that prints “Go big or go home!” unlimited times:

``````def EndlessPrint():
print("Go big or go home!")
EndlessPrint()

EndlessPrint()
``````

(Edit: This method doesn’t need for or while loops.)
I know, people haven’t met the function module, but it is an infinite loop type!
Codecademy developers’ attention to creating websites these days!

Thank you for the response. I really appreciate it.

Writing for posterity…

The reason that @array4507277167’s code doesn’t work is because the for loop runs the code block each time.

Notice that the block includes the declaration of lista, as an empty list. So, each time the block is running, it empties lista, and then adds whichever element it was on in the loop.

Thus, the only return is the last 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) #This only prints student names from list ‘students_period_A’

#the following code will actually print both lists combined.

for student in students_period_A:
students_period_B.append(student)

for student in students_period_B:
print(student)

#You may also do it like this:
for student in students_period_A:
students_period_B.append(student)

print(students_period_B)

#You could get a weird print out where it appends (adds an element from list A to list B) then prints… if so, your code would be this:
for student in students_period_A:
students_period_B.append(student)
print(students_period_B)
#Here, you have left the print statement as part of the loop. So, for each iteration of the loop, it will add to the end of list B an element (students name) from list A and then it will print per element added, and then go back to the start of the ‘for loop’

1 Like

im a little confused with this can you help me?

from datetime import datetime
now = datetime.now()

print ‘%02d-%02d-%04d’ % (now.month, now.day, now.year)

# will print the current date as mm-dd-yyyy

In the infinite loop exercise, I tried fixing the code to combine the lists by writing it as:

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

Yet, all this does is print the strings from students_period_A. When I clicked “View Solution,” the code was exactly the same as I’d written it. What am I missing here?

To preserve code formatting in forum posts, see: [How to] Format code in posts

The print statement within the loop is just printing each student in `students_period_A`. Add a couple of print statements after the loop 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)
print(students_period_B)
``````

`# students_period_A is unchanged`
Output: [“Alex”, “Briana”, “Cheri”, “Daniele”]

`# students_period_B is mutated/modified with four new elements`
Output: [“Dora”, “Minerva”, “Alexa”, “Obie”, “Alex”, “Briana”, “Cheri”, “Daniele”]

1 Like

Thanks. Very new to all of this. I think I get it now.

this is because when you add 1 at the last index of the list, you just add 1 into the my_favorite_numbers. so the condition notices that there is still an integer in the list, it will add 1 again and again and again. the output will like this 4,8,15,16,42,1,1,1,1,1…

Thank you very baktron

Here’s the solution. I’ve added the variable “leng” but it wasn’t necessary.

leng = len(students_period_A)
index = 0

for student in range(leng):
students_period_B.append(students_period_A[index])
index += 1
print(students_period_B)

The solution I’ve got:

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)

The outcome: [‘Dora’, ‘Minerva’, ‘Alexa’, ‘Obie’, ‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]
but doing it in reverse (appending B to the A) also works

@design6440643451 the way you wrote your loop is as if you are writing a while loop. While loops require the index and increment, but for loops you just give it a list to iterate. You are making it unnecessarily complicated through declaring bunch of variables.

students_period_A = ["Alex", "Briana", "Cheri", "Daniele"] students_period_B = ["Dora", "Minerva", "Alexa", "Obie"] #For Loop for student in students_period_A: students_period_B.append(student) print(students_period_B) students_period_B = ["Dora", "Minerva", "Alexa", "Obie"] #resetting our list students_period_B #While Loop leng = len(students_period_A) index = 0 while index < leng: students_period_B.append(students_period_A[index]) index += 1 print(students_period_B)

@fight_dragons while I appreciate your answer, I believe your solution is incorrect in agreeing with the others on this exercise.

The exercise here is explicitly teaching about using loops and infinite loops. The instruction states:

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

Delete the line causing the infinite loop and fix it to accomplish the original goal of combining all students from `students_period_A` into `students_period_B` .

The solution asks for combining he two lists and given context implicitly ask it to be done using loops. The solution some of the others provided are not doing the combine or appending but simply printing the list of students from both periods, which is not the point of the exercise. Just thought I would point out the context and given your status as problem solver may lead to further confusions.

Hi! I hope this is the appropriate place to ask this question.

I was playing around to understand how the temporary value within `for` loops behaves in different contexts.

Initially, I expected the below code to produce an infinite loop:

``````list_A =  [1, 2, 3, 4, 5]

for increment in list_A:
print(increment)
increment -= 1
``````

What I thought would happen:

• `increment` starts out with a value of `0`
• `1` is what exists in index `0`, so this is what is printed
• `increment` is then deincremented by 1
• As the loop reaches the next iteration, `increment` is incremented by 1, meaning it arrives back at 0, the original state of the loop. Repeat until heat death.

I understand now that the value assigned to the temporary variable is whatever exists at index X, rather than the index itself. Therefor, `increment -= 1` does not change the position of a given iteration.

My question - is it possible to manipulate the value/position of the temporary value beyond what happens automatically with each iteration? The above code doesn’t achieve that, but I’m curious to know if it’s possible to access and manipulate the temporary value itself.