Lets say I have 2 lists:
L1 = [2,4,1,6]
L2 = [1,5,2,3]
The output should be a new list that contains the biggest numbers found in L1 or L2 based on their position.
Example output:
L3 = [2, 5, 2, 6]
How to do it ?
One of the possible solutions is to zip your lists, and then applying max operation element-wise, which can be obtained in python through call to functional map
L1 = [2,4,1,6]
L2 = [1,5,2,3]
L3 = map(max, zip(L1, L2)) # python2
L3 = list(map(max, zip(L1, L2))) # python3
or more pythonic through list comprehensions
L3 = [max(l1, l2) for l1, l2 in zip(L1, L2)]
or a bit shorter version using unpacking operation
L3 = [max(*l) for l in zip(L1, L2)]
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