Why does it say I have an infinite loop?

Question

Why does it say I have an infinite loop?

Answer

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.

9 Likes

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()

##Output…
None

2 Likes

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

7 Likes

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.

1 Like

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?

2 Likes

it makes an empty list

Thanks @amrutha_g and @stetim94. Still struggling to visualize but this works:

#define a list
start_list = [5, 3, 1, 2, 4]
#create an empty list to add to later
square_list = []

# for (each item) in (your list)
for num in start_list: #remember 'num' is arbitrary --> could be 'sassafras' if you want (just make sure you refer to the same (each item) definition later --> e.g.,  square_list.append(sassafras ** 2)
  #loop through (grab one item at a time from start_list --> append it to square_list --> move to the next item (repeat till the list ends)
  square_list.append(num ** 2)    #but don't just add each item --> that would be square_list.append(num) --> you want to add each item squared with square_list.append(num ** 2) 
  
  ##then let's sort the list
  square_list.sort()
  
  #and print it
  print square_list
2 Likes

why would want to sort the list over and over again? Why not just sort the list once after running the loop?

4 Likes

For loops in python are all about iteration

Not sure this is perfect but seems we have three things happening here:

for num in start_list:  #remember num is our variable
    square_list.append(num ** 2) #we are only looping through our variable here ...

(1.1) grab start_list[0] --> square it --> add result as new item in square_list[0]
(1.2) grab start_list[1] --> square it --> add result as new item in square_list[1]
(1.3) grab start_list[2] --> square it --> add result as new item in square_list[2]
(1.4) grab start_list[3] --> square it --> add result as new item in square_list[3]
(1.5) grab start_list[4] --> square it --> add result as new item in square_list[4]
# No more items to append --> square_list is populated with newly calculated items --> iteration complete --> move to next iteration

    square_list.sort()

(2) refer back to our square_list (which is now populated with the squared items --> sort alphabetically
# This is all good --> iteration complete --> move to next iteration

Note, the .sort() method only happens once --> what you were saying about sorting mulitiple times only happens if somehow we added sort into our first iteration (1) --> for example:
(1.1) grab start_list[0] --> square it --> add result as new item in square_list[0] --> sort square_list --> move to (1.2)

    print square_list

(3) refer back to square_list (now both populated and sorted) --> print that monkey
# All iterations are complete (the loop ends)

Our goal is realized = make it all happen within a single loop …

If your .sort() is indented and located within your for loop it will sort every iteration, as it completes all instructions within the for loop before moving to the next num in the sequence(start_list). You can see proof of this displayed if you also moved your print() function into your for loop. Then you can see that it sorts for every iteration if the sort is in the for loop. If print is in the loop, it would print every iteration as well.

Pretty sure this statement is accurate. If not, someone feel free to correct me as I’m still(always) learning.

EDIT Of course, I could be mistaking what you are saying. Always a possibility. Good luck!

Does anyone know what is wrong with this code? This is what returned: Traceback (most recent call last):
File “python”, line 9, in
TypeError: unsupported operand type(s) for ** or pow(): ‘list’ and ‘int’

Code Below:
start_list = [5, 3, 1, 2, 4]

square_list =

Your code here!

import math

x = start_list

for number in start_list:

square_list.append = [x ** 2]

square_list.sort()

print square_list

Have a quick look at this FAQ which covers code formatting and indetnation on forum posts.

As for the error code it seems quite succint. On line 9 of your program you have used the operand ** with an integer and a list. I’m guessing that one of your names is a list when you actually meant to reference something else.

1 Like

Thanks for a detail explanation… it was very helpful in wrapping my head around that!

About this same example:
I used an extra print line to see in what order the program accesses the list, but it doesn’t go from the beginning to the end of the list; it instead reads the 2nd, then the 1st, then the 3rd, then the 5th and finally the 4th list item. Can anyone explain why that is? Or have I messed the code up somehow?

CODE

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

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

THE OUTPUT:
3
5
1
4
2
[25, 9, 1, 4, 16]

P.S.: The indentation doesn’t appear in the post but is there in the code so the program works fine; just wondering about the weird order of accessing the list items.

I ran your code your code, and the output is as expected:

5
3
1
2
4
[1, 4, 9, 16, 25]

so not sure what you did

Ah, great, thank you! Then I’ll just assume that was a weird hiccup.

1 Like

I was playing around with a few python functions.
i have a list on a text file below:

google
bing
yahoo
sky
bat
etc

And i copied the above text and paste into the list i made on the python terminal below:

my_list = [“google
bing
yahoo
sky
bat
etc”]

finallist = [ ", “.join(my_list.split(” ")) for item in my_list ]

print(finallist)

#console output:
my_list = ["google
^
SyntaxError: unterminated string literal (detected at line 2)

i want it looks like this:

["bing, yahoo, sky, bat, etc,"]

if i have an existing list eg. over 1000 on text format and copied it without entering it into the terminal one after the other and also making the list be the same line and also placing the required syntex “,” after each list
how can i resolve?

The above is a list with only one string element.

['bing', 'yahoo', 'sky', 'bat', 'google']

That above is now a list of str objects. Note that each is in its own set of quotes.