Short Python Code to say "Pick the lower value"?
Asked Answered
J

8

31

What I mean is, I'm looking for really short code that returns the lower value. for example:

a=[1,2,3,4,5,6,7,8,9,10]
b=[1,2,3,4,5,6,7,8]
len(a) = 10
len(b) = 8
if (fill-this-in):
     print(lesser-value)

And I forgot to add that if b is lower than a, I want b returned - not len(b) - the variable b.

Jukebox answered 11/1, 2009 at 2:21 Comment(0)
N
52
print(min(a, b))
Ninetta answered 11/1, 2009 at 2:30 Comment(0)
I
29

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]
Ikkela answered 11/1, 2009 at 4:21 Comment(1)
+1 for thoroughness :) althouth the question should really be fixedOmalley
S
5

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.

Spinning answered 11/1, 2009 at 4:49 Comment(0)
C
4

min() should accomplish what you need

print(min(a,b))
Cankerous answered 11/1, 2009 at 2:30 Comment(0)
A
2

It seems this answer may now be out of date. I just had this same question and found this answer, but wasn't getting the results I expected. Turns out Min doesn't automatically return the shorter of the two lists (in 2.7). To get that you have to use the 'key' argument (introduced in 2.5)(emphasis added):

min(iterable[, key]) min(arg1, arg2, *args[, key]) Return the smallest item in an iterable or the smallest of two or more arguments.

If one positional argument is provided, iterable must be a non-empty iterable (such as a non-empty string, tuple or list). The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.

The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

Changed in version 2.5: Added support for the optional key argument

So in this example, although it seems to work (and still would in 2.7), it only does because the list of integers is the same. However if these were two different non-ordered lists then:

min(a,b) 

would return the list with the lowest first integer.

To be sure to get the shorter of two lists, use:

min(a,b, key=len)
Arrival answered 2/11, 2016 at 17:32 Comment(1)
Damn, missed that @coady already more succinctly answered this.Arrival
S
1

heads up, min(a, b, key=len) only works in python 2.5 and up I think.

(it's not working on my macbook with python 2.4, but my linux server with 2.5 is fine)

Scagliola answered 14/1, 2009 at 3:56 Comment(0)
N
0

Is the following what you want?

if len(a) < len(b):
    print a
else:
    print b

Alternatively, if you want to use the ternary operator like @Andrew G. Johnson:

print a if len(a) < len(b) else b

PS. Remember that Python does not use braces for its blocks, and that its ternary operator is different from C-like languages.

Nari answered 11/1, 2009 at 2:43 Comment(0)
T
-3

I don't know Python but for something like this I'd use a ternary operator.

print(length(a) < length(b) ? length(a) : length(b))

One thing to note about this that if they are equal it will print length(b)

Thistly answered 11/1, 2009 at 2:38 Comment(1)
I didn't downvote you but, if you find yourself answering a python-tagged question starting with the immortal phrase "I don't know Python but ...", you may just want to think about not answering :-).Cartwright

© 2022 - 2024 — McMap. All rights reserved.