What would be an elegant way to map
a two parameter lambda
function to a list of values where the first parameter is constant and the second is taken from a list
?
Example:
lambda x,y: x+y
x='a'
y=['2','4','8','16']
expected result:
['a2','a4','a8','a16']
Notes:
You can use itertools.starmap
a = itertools.starmap(lambda x,y: x+y, zip(itertools.repeat(x), y))
a = list(a)
and you get your desired output.
BTW, both itertools.imap
and Python3's map
will accept the following:
itertools.imap(lambda x,y: x+y, itertools.repeat(x), y)
The default Python2's map
will not stop at the end of y
and will insert None
s...
But a comprehension is much better
[x + num for num in y]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With