using map function to square a list
<p>I have a list which I want to square each of them.</p>

<p>this is what I have done</p>

<pre><code>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))
</code></pre>

<p>but the answer is this</p>

<pre><code>[,1,,, ,4,,, ,9,,, ,1,6,,, ,2,5,,, ,3,6,]
</code></pre>

<p>instead of </p>

<pre><code>[1,4,9,16,25,36]
</code></pre>

<p>The problem is in these lines:</p>

<pre><code>squares_as_strings = str(squares)
print(','.join(squares_as_strings))
</code></pre>

<p>Applying <code>str</code> to the <code>squares</code> array does not make each number a string.
Instead it gives this - <code>'[1, 2, 3]'</code>, which is a string, not an array of strings as you want.</p>

<p>Now, you are <code>join</code>-ing it, which results in a comma between each character.</p>

<p>You should instead do:</p>

<pre><code>squares = map(str, map(square, numbers))
</code></pre>

<p>This would make each element of the <code>squares</code> array a string. And then use your join.</p>

<hr>

<pre><code>def square(x):
    return x * x

nums = [1, 2, 3]
squares = map(str, map(square, nums))

print(','.join(squares))
</code></pre>

python string list