Python3: Using ternary operator on map it will return None
For python 2.7.13
>>> lst_ = [1]
>>> map(lambda x, y: x + y if y else 1, lst_, lst_[1:])
[1]
For python 3.6.1
>>> lst_ = [1]
>>> list(map(lambda x, y: x + y if y else 1, lst_, lst_[1:]))
[]
Two questions:
-
I want to know why python2 is returning the correct result, but python3 is returning None
-
How do I change my code using python3 to return the correct result
source to share
This is a change in the functionality of a function map()
(along with a change to become an iterator). Since inputs are now iterators, map()
updated to follow the same behavior zip()
as and not shorter value inputs None
.
Compare the documentation for map()
in Python 2 :
If one iterable is shorter than the other, it is assumed to be extended with elements
None
.
with Python 3 version :
When using multiple iterations, the iterator stops when it runs out of the shortest iterative file.
You can use itertools.zip_longest()
together with itertools.starmap()
get Python 2 behavior again:
from itertools import starmap, zip_longest
starmap(lambda x, y: x + y if y else 1, zip_longest(lst_, lst_[1:]))
zip_longest()
has the added benefit that you can now specify which value to use as filler; as an example you can set it to 0
:
starmap(lambda x, y: x + y, zip_longest(lst_, lst_[1:], fillvalue=0))
Demo:
>>> from itertools import starmap, zip_longest
>>> lst_ = [1]
>>> list(starmap(lambda x, y: x + y if y else 1, zip_longest(lst_, lst_[1:])))
[1]
>>> list(starmap(lambda x, y: x + y, zip_longest(lst_, lst_[1:], fillvalue=0)))
[1]
source to share