Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing list with the largest value

I have two lists of numbers and I want a function to return the list with the largest number i.e with two lists [1,2,3,9] and [4,5,6,7,8], the function should return [1,2,3,9].

I know for a fact that this works:

a = [1,2,3,9]
b = [4,5,6,7,8]
ans = [_ for _ in [a,b] if max(_) == max((max(a),max(b)))][0]

I know that there is:

a = [1,2,3,9]
b = [4,5,6,7,8]
if max(a)>max(b):
    ans = a
else:
    ans = b

But is there a more efficient one or two line solution?

like image 288
Wan-Hew Tran Avatar asked Dec 13 '22 12:12

Wan-Hew Tran


1 Answers

How about using the following without any for loop. Just compare the max of lists

a = [1,2,3,9]
b = [4,5,6,7,8]
ans = (a if max(a) > max(b) else b)
# [1, 2, 3, 9]
like image 111
Sheldore Avatar answered Dec 30 '22 03:12

Sheldore