Omar Khaled
Omar Khaled

Reputation: 43

Comparing lists by min function

I was trying to compare different lists and to get the shorter one among them with the min() built-in function (I know that this isn't what min() made for but I was just trying) and I've got some results that made me not sure what the output was based on

min(['1','0','l'], ['1', '2'])
>>> ['1', '0', 'l']
min(['1','2','3'], ['1', '2'])
>>> ['1', '2']
min(['1', '2'], ['4'])
>>> ['1', '2']
min(['1', 'a'], ['0'])
>>> ['0']
min(['1', 'a'], ['100000000'])
>>> ['1', 'a']

I don't know what is the output based on and I hope someone can help me and clarify this behavior.

Upvotes: 1

Views: 1216

Answers (4)

YousefZ
YousefZ

Reputation: 345

You can check what an inbuilt function does in the documentation

as you can see the minimum function accepts two things as its parameters:

  1. min(iterable, *[, key, default]) : which is used to get the smallest value in an iterable object such as a list.
  2. min(arg1, arg2, *args[, key]): which is what you are current using. It gets the minimum value when both arguments are compared. When comparing lists to see which one is smaller, it will get the first index that does not have the same value in both lists i.e.
a = [3,5,1]
b = [3,3,1]

result = a > b # true

here the first index that is not the same on both lists is index 1, and so the comparison is 5 > 3 (which is true)

using this logic of comparing lists, the min() function will return the list that has the smallest index which is unique and smaller than the other list.

See lexicographical order.

If you place characters, then we use lexicographical ordering, and so

>>> 'a' < 'b'
True
>>> '1' < '2'
True
>>> 'a' < 'A'
False

Upvotes: 1

Tim Woocker
Tim Woocker

Reputation: 1996

The min() function takes the keyword argument key which you can use to specify what exact value to compare. It is a function which gets the list in your case as the argument.

So to get the shortest list, you can use this code:

min(['1','0','l'], ['1', '2'], key=lambda x: len(x))

Regarding your code and how the min() function determines the result: You can look at your list like a string (which is just a list of characters in theory). If you'd compare a string to another one, you'd look at the letters from left to right and order them by their leftmost letters. For example abc < acb because b comes before c in the alphabet (and a=a so we can ignore the first letter).

With lists it's the same. It will go through the items from left to right and compare them until it finds the first one which is not equal in both lists. The smaller one of those is then used to determine the "smaller" list.

Upvotes: 2

user15681262
user15681262

Reputation: 106

min finds the 'smallest' of the lists by the comparison operator they provide. For lists, it works by lexicographical order - of two lists, the one whose first unequal(to the elements in the other list at the same index) element is larger, is the larger list.

Upvotes: 2

jvrn3
jvrn3

Reputation: 610

From the documentation:

Docstring:
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.

So, for example,

IN: min([5,4,3], [6])
OUT: [6]

As @Tim Woocker wrote, you should use a function(argument key) to specify what you want to compare.

Upvotes: 0

Related Questions