Question

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

I want to add start_list's elements and their sqrt in square_list. But it'll be infinite loop. (I guess, it happened in (start_list.append(x**2) ) How can i fix it?

Was it helpful?

Solution 2

The .append(start_list.append(x**2)) call should, if I understand what you're going for, not actually be append(append()) but just a lookup. Since you're iterating over your list items, the x value is the value you want to append to square_list.

for x in start_list:
    square_list.append(x**2)

However, the list comprehension technique is probably the 'righter' answer, as it's Pythonic and excellent.

OTHER TIPS

Never alter a list while iterating it. Use list comprehension instead, like this

start_list = [5, 3, 1, 2, 4]
print [x**2 for x in start_list]
# [25, 9, 1, 4, 16]

Actually, you are doing something like this

start_list = [5, 3, 1, 2, 4]
for item in start_list:
    start_list.append(item ** 2)

Lets put some print statements and understand what happens when you execute the code

start_list = [5, 3, 1, 2, 4]
for item in start_list:
    start_list.append(item ** 2)
    print len(start_list), start_list
    if len(start_list) == 15:
        break

Output

6 [5, 3, 1, 2, 4, 25]
7 [5, 3, 1, 2, 4, 25, 9]
8 [5, 3, 1, 2, 4, 25, 9, 1]
9 [5, 3, 1, 2, 4, 25, 9, 1, 4]
10 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16]
11 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16, 625]
12 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16, 625, 81]
13 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16, 625, 81, 1]
14 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16, 625, 81, 1, 16]
15 [5, 3, 1, 2, 4, 25, 9, 1, 4, 16, 625, 81, 1, 16, 256]

You are basically growing the list on every iteration by appending to it. That is why your program is in infinite loop.

Another problem is that I think .append() returns None, so you aren't really appending anything to square_list when you do:

square_list.append(start_list.append(x**2))

Thought it might help solve your problem in addition to thefortheye's answer.

The return value of start_list.append(x**2) will be None, so the element in square_list will be [None, None, None, ...], and because you append item to start_list during every iteration, so it will get an infinite loop.

I think what you want to do would like this:

l = [5, 3, 1, 2, 4]
l += [x**2 for x in l]
l.sort()
print(l)
# without changing the code much
start_list = [5, 3, 1, 2, 4]
square_list = []
for x in start_list.copy():
    square_list.append(x**2) # appending returns a None value rather the appended value 
    start_list.append(x ** 2) # not sure why you want to append the root into the start list
    square_list.sort() # sort square_list
    print square_list

# better way of doing it
start_list [5, 3, 1, 2, 4]
square_list = [ x ** 2 for x in start_list]
print square_list

If you want to print it like it is in a for loop you could do [(square_list.sort(), print(square_list[0: x+1])) for x in range(len(square_list))] but I think it only works in python 3 since print is a function and not a statement like in python 2(as far as I know.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top