Do this:
list(map(chr,[66,53,0,94]))
In Python 3+, many processes that iterate over iterables return iterators themselves. In most cases, this ends up saving memory, and should make things go faster.
If all you're going to do is iterate over this list eventually, there's no need to even convert it to a list, because you can still iterate over the map object like so:
# Prints "ABCD"
for ch in map(chr,[65,66,67,68]):
print(ch)
Answer from Kenan Banks on Stack OverflowWhat is map function in Python?
Map not working
python - What is a mapping object, according to dict type? - Stack Overflow
The map() function or the list comprehension?
Videos
Do this:
list(map(chr,[66,53,0,94]))
In Python 3+, many processes that iterate over iterables return iterators themselves. In most cases, this ends up saving memory, and should make things go faster.
If all you're going to do is iterate over this list eventually, there's no need to even convert it to a list, because you can still iterate over the map object like so:
# Prints "ABCD"
for ch in map(chr,[65,66,67,68]):
print(ch)
New and neat in Python 3.5:
[*map(chr, [66, 53, 0, 94])]
Thanks to Additional Unpacking Generalizations
UPDATE
Always seeking for shorter ways, I discovered this one also works:
*map(chr, [66, 53, 0, 94]),
Unpacking works in tuples too. Note the comma at the end. This makes it a tuple of 1 element. That is, it's equivalent to (*map(chr, [66, 53, 0, 94]),)
It's shorter by only one char from the version with the list-brackets, but, in my opinion, better to write, because you start right ahead with the asterisk - the expansion syntax, so I feel it's softer on the mind. :)
I need good explanation with example. Thanks
From the source code for CPython, this comment:
/* We accept for the argument either a concrete dictionary object,
* or an abstract "mapping" object. For the former, we can do
* things quite efficiently. For the latter, we only require that
* PyMapping_Keys() and PyObject_GetItem() be supported.
*/
So, "the minimal interface required for dict(mapping) to work" appears to be .keys() and .__getitem__().
Example program:
class M:
def keys(self):
return [1,2,3]
def __getitem__(self, x):
return x*2
m = M()
d = dict(m)
assert d == {1:2, 2:4, 3:6}
The glossary defines it as:
A container object that supports arbitrary key lookups and implements the methods specified in the Mapping or MutableMapping abstract base classes. Examples include
dict,collections.defaultdict,collections.OrderedDictandcollections.Counter.
So it looks like the minimal list of methods to meet the definition is __getitem__, __iter__, __len__, __contains__, keys, items, values, get, __eq__, and __ne__. Although I bet the dict constructor does not actually need all of those.