Yusuf Ning - 8 months ago 71

Python Question

I have a list which I want to square each of them.

this is what I have done

`def square(x):`

return x*x

numbers = [1,2,3,4,5,6]

squares = map(square, numbers)

squares_as_strings = str(squares)

print(','.join(squares_as_strings))

but the answer is this

`[,1,,, ,4,,, ,9,,, ,1,6,,, ,2,5,,, ,3,6,]`

instead of

`[1,4,9,16,25,36]`

Answer

You map the `square`

function, but you're not mapping the `str`

function. Your `squares_as_strings`

is one string, of the entire list.

Change `str(squares)`

to `map(str, squares)`

.