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?
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.