How to print map object with Python 3?


This is my code

def fahrenheit(T):
    return ((float(9)/5)*T + 32)

temp = [0, 22.5, 40,100]
F_temps = map(fahrenheit, temp)

This is mapobject so I tried something like this

for i in F_temps:

<map object at 0x7f9aa050ff28>
<map object at 0x7f9aa050ff28>
<map object at 0x7f9aa050ff28>
<map object at 0x7f9aa050ff28>

I am not sure but I think that my solution was possible with Python 2.7,how to change this with 3.5?

Asked By: MishaVacic



You have to turn the map into a list or tuple first. To do that,


This is because maps are lazily evaluated, meaning the values are only computed on-demand. Let’s see an example

def evaluate(x):

mymap = map(evaluate, [1,2,3]) # nothing gets printed yet
print(mymap) # <map object at 0x106ea0f10>

# calling next evaluates the next value in the map
next(mymap) # prints 1
next(mymap) # prints 2
next(mymap) # prints 3
next(mymap) # raises the StopIteration error

When you use map in a for loop, the loop automatically calls next for you, and treats the StopIteration error as the end of the loop. Calling list(mymap) forces all the map values to be evaluated.

result = list(mymap) # prints 1, 2, 3

However, since our evaluate function has no return value, result is simply [None, None, None]

Answered By: codelessbugging
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.