Short Python Code to say "Pick the lower value"? Short Python Code to say "Pick the lower value"? python python

Short Python Code to say "Pick the lower value"?


You're not hugely clear about what you want, so some alternatives. Given the following two lists:

a = [1,2,3,4,5,6,7,8,9,10]b = [1,2,3,4,5,6,7,8]

To print the shortest list, you can just do..

>>> print(min(a, b))[1, 2, 3, 4, 5, 6, 7, 8]

To get the shortest length as an number, you can either min the len() of each list, or do len(min()) (both are identical, choose which ever you find most readable)..

>>> print(min( len(a), len(b) ))# or..>>> print(len( min(a, b) ))8

To print the lowest value in either list, you can supply the list as a single argument to min()

>>> a.extend(b) # Appends b to a>>> print a[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8]>>> print(min(a))1

Finally, another possibility, the list that has the lowest values in total:

>>> max( sum(a), sum(b) )55

To print the actual list with the highest sum(), you could either use the ternary operator, like..

>>> print a if sum(a) > sum(b) else b[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

..although I never really liked (or use) it, instead using the slight longer, regular if/else statements..

>>> if sum(a) > sum(b):...     print a... else:...     print b... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


If the length of the list is what makes it lower (not its values), then you actually want:

min(a, b, key=len)

which is only incidentally equivalent to

min(a, b)

in the given example.