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?
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]
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