Can anybody help me with this. I don't know what am I doing wrong over here
list = [-9, -2, 0, 2, 3]
squared_list=[]
for element in list:
squaring = list[element] * list[element]
squared_list.append(squaring)
squared_list.sort()
print(squared_list)
CodePudding user response:
Use squaring = element * element
against squaring = list[element] * list[element]
because for the first element, for example, you're consulting list[-9] and that index is out.
CodePudding user response:
Solution
list = [-9, -2, 0, 2, 3]
squared_list=[]
for element in list:
squaring = element**2
squared_list.append(squaring)
squared_list.sort()
print(squared_list)
OR shorter version :)
list = [-9, -2, 0, 2, 3]
squared_list=[element**2 for element in list]