Mr_Null_Byte Mr_Null_Byte - 5 months ago 45
Python Question

python map function error

i am trying to convert all the numbers in a list to their squares using a lambda function and map function,but it doesn't seem to gimme the correct output! Here is the code:

num_file = [12,24,36]
map(lambda num:num**2,num_file)

But it gives me this output instead of the squares :

<map at 0x4d82d10>

please Help !

Answer Source

map returns a map object, to get a list out of it, use the list function:

>>> num_file = [12,24,36]
>>> list(map(lambda num:num**2,num_file))
[144, 576, 1296]

You can also use a list comprehension:

>>> num_file = [12,24,36]
>>> [num**2 for num in num_file]
[144, 576, 1296]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download