Home > Mobile >  Selecting the highest number between two elements in an array
Selecting the highest number between two elements in an array

Time:06-22

I have multiple 2-D arrays as defined below:

a=[[4,6],[7,8],[9,10]]

I want to create a separate array(1-D) that takes the maximum number between the two elements in the array above. The solution should look like the below:

a=[6,8,10]

Any suggestions would be helpful. Thank you.

CodePudding user response:

There is a simple list comprehension for this:

a = [[4,6],[7,8],[9,10]]
b = [max(elem) for elem in a]

CodePudding user response:

Use a list comprehension and take the max value of each entry in the outer list.

a = [[4, 6], [7, 8], [9, 10]]
result = [max(entry) for entry in a]
print(result)

This will give you [6, 8, 10].

  • Related