Square_list = [] but why?


#1



https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096


I see how to make the code work - squaring the numbers and sorting them- I just don't understand why after your array of numbers you make another variable called
square_list = []

Not sure, but this seems to make sure you do not have an infinite loop. How do you know you need to add this variable? How does it work?

For example if I try to do this exercise WITHOUT square_list = [] I get an error message > The program took too long to finish. Check your code for infinite loops and try again

start_list = [5, 3, 1, 2, 4]

for item in start_list:
     start_list.append(item ** 20)
     start_list.sort()

print start_list

This gives error : > The program took too long to finish. Check your code for infinite loops and try again

Then when typed correctly with square_list = [] under your array it works

start_list = [5, 3, 1, 2, 4]
square_list = []

for item in start_list:
     square_list.append(item ** 2)
     square_list.sort()

print square_list

This works - but why what does making a nother variable with an empty array do? This tells it loop one time?? How would you know you need to add extra empty variables and call them into your for loop?



#2

hm... this is an extensive program. You keep adding items to the list you are looping over, making the loop longer (actually infinite). and after each time you added an item, lets sort the list.

appending to a different list, doesn't make the list you are currently looping over infinitely big


#3

I'm fairly amateur myself, but from what I understand — basically, in your second version of code you tell the for to go through items from start_list, square them and append the results to square_list.

Now, look at your first version of this code and answer this question: if the code squares every item in the list and appends it to start_list, how will start_list look after your code iterates through 5, 3, 1, 2 and 4? Will it have more items? If yes, will this code square these new items as well and fall into an infinite loop, or will it just stop somewhere?


#4

Ok so with more understanding without the empty array the original array start_list will keep expanding as another item is added to the array and it keeps going though and squaring items -adding that squared item to the end of the array, squaring the next item and so on and on.
By appending the items into the empty array and iterating over the original array it finishes going through the items and they are dropped/appended into that empty array which we see when we call it to print. :smile:


#5

yes, you seem to understand it now. The only thing i would change is this:

square_list = []

for item in start_list:
     square_list.append(item ** 2)
square_list.sort()

print square_list

this way, the list is sorted after the loop, instead of each run of the loop.


#6

yes I agree or sort before your run the loop


#7

yea, given now every number gets the same math operations, sorting can be done before or after, but in each run of the loop (like you did) is not very efficient


#8

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