[docs] Python v3.2.3 - Help me!

Georg Brandl georg at python.org
Sun Jul 1 09:54:11 CEST 2012


On 29.06.2012 17:31, focajoca at sapo.pt wrote:
> Hello, to whom can help me!
>
> I'm a beginner in programming.
> I have a Toshiba Tecra A3 (1.60 GHz Intel Pentium M), and I use python v3.2.3.
> I'm testing the map function,
> /map(function, iterable...)/
> but I do not get the expected result, as you see below:
>
> Test1:
>  >>> m=[10,20,30,40]
>  >>> map(str,m)
> <map object at 0x01007AD0>
> The expected result would be: ['10','20','30','40']
>
> Test2:
>  >>> def double(x):
> return 2*x
>
>  >>> map (double,m)
> <map object at 0x01007A70>
> The expected result would be: [20,40,60,80]
>
> Where am I going wrong? Can you help?

Hi Carlos,

in Python 3 a few functions such as map() and zip() have been changed to
return not whole lists, but iterators.  You can make a list out of them
by wrapping the result in list(), e.g.

 >>> list(map(double, m))
[20, 40, 60, 80]

But in many cases this is not necessary, for example when iterating over
the result, e.g.

 >>> for dbl in map(double, m):
...     print(dbl)
20
40
60
80

and it is usually more efficient without the list().  (That's the reason
the functions were changed in the first place.)

cheers,
Georg


More information about the docs mailing list