Chuck Black
Chuck Black

Reputation: 13

Maximum value for each position in multiple lists

I need to make a list comprehension consisting of the maximum values for each position. For L1, L2, and L3, I want to get [4, 5, 3, 5]. Here's what I've got so far:

L1 = [1, 2, 3, 4]
L2 = [4, 3, 2, 3]
L3 = [0, 5, 0, 5]

maxs = []

L4 = list(zip(L1,L2,L3))

print (maxs)

Upvotes: 1

Views: 175

Answers (1)

user2390182
user2390182

Reputation: 73460

The following comprehension will do:

>>> [max(tpl) for tpl in zip(L1, L2, L3)]
[4, 5, 3, 5]

You can also use map to apply the max function to all the zipped tuples:

>>> list(map(max, zip(L1, L2, L3)))
[4, 5, 3, 5]

Upvotes: 2

Related Questions