map
with multiple iterables always iterates all of them in parallel and stops when the shortest iterable is exhausted. So,
map(test, list1, [list2])
will compute test(list1[0], list2)
and then immediately stop, since [list2]
only contains one entry.
Depending on your use case, there’s a few ways you could go about handling this.
1. Extend the arguments to map
As noted, map
terminates because [list2]
only contains one entry. You can band-aid that by instead passing an iterable with many duplicate entries of list2
(at least as long as list1
, or possibly infinitely long):
>>> list(map(test, list1, [list2] * len(list1)))
[15, 6, 21, 18, 3]
>>> import itertools
>>> list(map(test, list1, itertools.repeat(list2)))
[15, 6, 21, 18, 3]
2. Partially apply test
ahead of time
If you always want to pass the same list as the second argument to test
, you can instead create a new version of the test
that pre-supplies list2
as the argument for myList
. That can be done either by defining a new function or using functools.partial
:
import functools
def test2(myVal):
return test(myVal, list2)
test3 = functools.partial(test, myList=list2)
Both can be used in the same way:
>>> list(map(test2, list1))
[15, 6, 21, 18, 3]
>>> list(map(test3, list1))
[15, 6, 21, 18, 3]