1

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.

  • 1
    If you want to get the smallest list, you need to compare the length of the lists, not the lists directly. Use the `len()` function for that: `min(len([...]), len([...]))` – Tim Woocker Apr 20 '21 at 14:13
  • 1
    It compares the element present in the 2 lists lexicographically. – Vishal Singh Apr 20 '21 at 14:13
  • Yes I tried that but That gave me the shortest length and what I want is the shortest list – Omar Khaled Apr 20 '21 at 14:15
  • Does this answer your question? [Comparing two lists using the greater than or less than operator](https://stackoverflow.com/questions/13052857/comparing-two-lists-using-the-greater-than-or-less-than-operator) The `min` function uses the same logic as the `==`, `>` and `<` operators to find the minimum value – Pranav Hosangadi Apr 20 '21 at 14:22

4 Answers4

2

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.

Tim Woocker
  • 1,883
  • 1
  • 16
  • 29
2

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.

user15681262
  • 106
  • 1
  • 3
1

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
YousefZ01
  • 315
  • 3
  • 9
0

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.

jvrn3
  • 600
  • 1
  • 5
  • 18