Why does it say I have an infinite loop?



Why does it say I have an infinite loop?


If you wrote something along the lines of start_list.append(...) inside of your for loop, then you have created an infinite loop.
Think about it like this:
Our loop goes over each item in start_list
It enters the loop and appends a number to start_list
Loop repeats as next number in start_list and then appends another number to that same list
Do you see the pattern here? That method infinitely appends items to the list and can never possibly exit the loop, thus the infinite loop error message!
To fix this, be sure to .append() items to the square_list, which exists solely to hold your squared values!
Also, be sure to .sort() your list after the loop, not inside of it. Sorting it each time the loop runs is a costly operation, we only need to sort it once after everything has been appended. And make sure you are sorting the square_list.

FAQ: Learn Python: Python Lists and Dictionaries - More with 'for'

I have a doubt in the output. Why is it showing “None” when we are printing ‘square_list’ … As we are appending the items into it it should display the values but why is it displaying none??
Can you please answer me the reason…?

Check out my Code…

start_list = [5, 3, 1, 2, 4]
square_list = []
for number in start_list:
square_list.append(number ** 2)
print square_list.sort()



.sort() modifies the list, and returns None. So you will to sort first, and then print. Otherwise you print the returned result


Worked…Thank You for answering and also for responding soon


if you really want to print and sort on the same line, use sorted() function, but this will give you a copy.


Okay… I have no idea about that before…


good day to all, i got question…
in beginning of code:
start_list = [5 ,3 ,1 ,2 ,4 ]
square_list = []

why square_list with empty brackets are stated, what does are their functions?


it makes an empty list