7 Comments
User's avatar
Ed Silva's avatar

data = [1, 2, 3, 4]

def square_list(lst):

y = []

for x in (range(len(lst))):

y.append(lst[x] ** 2)

return(y)

n = square_list(data)

print(n)

Expand full comment
Ardit Sulce's avatar

Perfect!

Expand full comment
Ardit Sulce's avatar

My only remark would be to use better variable names versus single letters such as "n".

Expand full comment
Ardit Sulce's avatar

Hi, I couldn't see the completed code in your trinket link.

Expand full comment
Vinod VV's avatar

I have done mine. Here is my code....

data = [1, 2, 3, 4]

def square_list(lst):

return [x**2 for x in lst]

squared_list = square_list(data)

print(squared_list)

Output:-

[1, 4, 9, 16]

Expand full comment
Ardit Sulce's avatar

Very good use of list comprehension! Good job!

Expand full comment