Python Forum
I don't understand this result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I don't understand this result (/thread-2594.html)



I don't understand this result - Ponomarenko Pavlo - Mar-27-2017

>>> a = [1, 2, 4, 5]
>>> def az():
            x * 2

>>> q = map(az, [2, 3, 4])
>>> list(q)
[None, None, None]



RE: I don't understand this result - zivoni - Mar-27-2017

You forgot to return the value computed in your function:

>>> def az(x):
...   return x*2
... 
>>> q = map(az, [2, 3, 4])
>>> list(q)
[4, 6, 8]



RE: I don't understand this result - Larz60+ - Mar-27-2017

This code is incomplete.
the definition of az is was missing it's body ok now


RE: I don't understand this result - buran - Mar-27-2017

just to mention that there is no need to explicitly convert the result to list
>>> def az(x):
return x*2

>>> q = map(az, [2, 3, 4])
>>> q
[4, 6, 8]



RE: I don't understand this result - snippsat - Mar-27-2017

(Mar-27-2017, 12:10 PM)buran Wrote: just to mention that there is no need to explicitly convert the result to list
Python 3 has changed a lot buran Wink
# Python 3.6
>>> def az(x):
...     return x*2
... 
>>> q = map(az, [2, 3, 4])
>>> q
<map object at 0x049C8EB0>
>>> list(q)
[4, 6, 8]
# Python 2.7
>>> def az(x):
...     return x * 2
...     
>>> q = map(az, [2, 3, 4])
>>> q
[4, 6, 8]
Built-ins like range, map, zip, filter become iterables in 3.x to conserve space,
rather than producing a result list all at once in memory.


RE: I don't understand this result - buran - Mar-27-2017

(Mar-27-2017, 01:53 PM)snippsat Wrote: Python 3 has changed a lot buran Wink

Built-ins like range, map, zip, filter become iterables in 3.x to conserve space,
rather than producing a result list all at once in memory.
Funny part is - I know it, but I tend to think python2 more often than python3