The signature for map is
map(function, iterable[, iterables[, ...]])
In Python 2.x if function is None identity is assumed, and short iterables are padded with ‘None’ to the length of the longest iterable.
In Python 3.x if function is None you eventually get an exception:
TypeError: 'NoneType' object is not callable
and all iterables are trimmed to the length of the shortest one.
That’s a couple of pretty drastic changes. How do I get 2.x semantics back?
Oh, and it now returns an iterator instead of a list, but I’m okay with that change. 😉
This is useful for cases where you don’t know ahead of time which function, if any, will be applied — just because you aren’t actually transforming the iterable doesn’t mean you don’t want its contents.
You must roll your own — but it’s easy:
A simple example:
which gives us:
If you have only one iterable, like:
Then simply replace that with:
Update:
My original answer here didn’t work if you called
map()withNoneas function and only one iterable. That’s because nobody does that, since then you can just as well just uselist(iterable). But I added the handling of that case anyway, by popular request. 🙂