I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:
A: Python 2.6:
>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
However, in Python 3.1, the above returns a map object.
B: Python 3.1:
>>> map(chr, [66, 53, 0, 94])
<map object at 0x00AF5570>
How do I retrieve the mapped list (as in A above) on Python 3.x?
Alternatively, is there a better way of doing this? My initial list object has around 45 items and id like to convert them to hex.
I'm not familiar with Python 3.1, but will this work?
means it will return an iterator.
means that the next() function of the iterator will take one value of each iterables and pass each of them to one positional parameter of the function.
So you get an iterator from the map() funtion and jsut pass it to the list() builtin function or use list comprehensions.
List-returning map function has the advantage of saving typing, especially during interactive sessions. You can define
lmap
function (on the analogy of python2'simap
) that returns list:Then calling
lmap
instead ofmap
will do the job:lmap(str, x)
is shorter by 5 characters (30% in this case) thanlist(map(str, x))
and is certainly shorter than[str(v) for v in x]
. You may create similar functions forfilter
too.There was a comment to the original question:
It is possible to do that, but it is a very bad idea. Just for fun, here's how you may (but should not) do it: