Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2 vs Python 3 - Difference in map behavior with three arguments?

The following code behaves differently in Python 2 vs Python 3:

all(map(lambda x,y: x, [1, 2], [1, 2, 3]))

Python 2 gives False whereas Python 3 gives True. The documentation for Python 2 says that it will supply None if the shorter list is exhausted but Python 3 doesn't do that.

I am working on a code that really needs the length to be maintained for some reason. What is the cleanest way to get the old behavior? I know I can use from past.builtin import map as old_map, but is there a more elegant solution that would work in both versions?

like image 796
Nishant Avatar asked Dec 10 '25 22:12

Nishant


1 Answers

Essentially, map with multiple iterables for the arguments will zip the iterables, and then call the function with the tuples from the zip as var-args. So, you can get the same behaviour using itertools.starmap and zip:

>>> a = [10, 20]
>>> b = [1, 2, 3]
>>> f = lambda x, y: x
>>> list(map(f, a, b))
[10, 20]
>>> from itertools import starmap
>>> list(starmap(f, zip(a, b)))
[10, 20]

Then the behaviour you want can be achieved by replacing zip with itertools.zip_longest:

>>> from itertools import starmap, zip_longest
>>> list(starmap(f, zip_longest(a, b)))
[10, 20, None]

Both functions from itertools also exist in Python 2, except the second one is named izip_longest instead. You can just import ... as ... to get around that.

like image 61
kaya3 Avatar answered Dec 13 '25 12:12

kaya3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!