0
list1=[-4,-1,0,3,10]
for k in list1:
    list1.append(k**2)

I am trying to find the square of the numbers in list1 and then I am trying to add the square to the same list1. But it is going to infinity loop. Can some1 help me here

2 Answers2

0

may be something like this

list1=[-4,-1,0,3,10]
list2 = []
for k in list1:
    list2.append(k**2)
list1 = list1 + list2
print(list1)

Or This

list1=[-4,-1,0,3,10]
list1 = list1 + [k**2 for k in list1]
print(list1)

http://ideone.com/vgZlXl

Nafis Islam
  • 1,483
  • 1
  • 14
  • 34
0

Try this

>>> list1 = [-4, -1, 0, 3, 10]
>>> list1.extend([x**2 for x in list1])
>>> list1
[-4, -1, 0, 3, 10, 16, 1, 0, 9, 100]
abhilb
  • 5,639
  • 2
  • 20
  • 26