orangewall14
orangewall14

Reputation: 1

How do I return an element from an array with the largest specific value in it?

array = [['A', '12', '12'], ['B', '12', '102']]

y = 1
for subarray in array:

    if subarray[2] > subarray[y][2]:
        print(subarray[0])

    y += 1

I wish to return the first element of the subarray with the highest final value (subarray[2]), in this example the program should return B because 102>12

Upvotes: 0

Views: 30

Answers (1)

yatu
yatu

Reputation: 88305

You could use max with a key to order based on the last element cast to int, and take the first element:

max(array, key=lambda x: int(x[2]))[0]
# 'B'

Upvotes: 2

Related Questions