0

Using Python 3.8

I am simply using min(list_of_list) and this seems to do the job.

lst_of_list = [[9,10,15], [1,8,4], [-3,999,9999]]
min(lst_of_list) = [-3,999,9999]

lst_of_list = [[26, 40, 83], [49, 60, 57], [13, 89, 99]]
min(lst_of_list) = [13, 89, 99]

and other cases worked as I wanted.

From lots of stackoverflow questions they do not simply use min(lst_of_list) and in the documentation it says. "The smallest item in the iterable is returned". So in lst_of_list each list would be an item so I thought min(lst_of_list) returns list with minimum sum, mean, etc... But this doesn't make sense either.

Does min(lst_of_list) find lowest value in list of lists and return list containing lowest value or is there something I am missing therefore this method is not appropriate?

martineau
  • 119,623
  • 25
  • 170
  • 301
haneulkim
  • 4,406
  • 9
  • 38
  • 80
  • 6
    "Smallest" is determined by doing `<`, so you should check the documentation or a tutorial to learn how lists are compared. – kaya3 Sep 15 '20 at 23:09
  • 1
    It's using list comparison for the elements of the top-level list. For example, try `[9,10,15] < [-3,999,9999]` and see what you get. It's doing element-wise comparison, much like the lexicographic comparison used to compare two strings. First it compares the first elements. If they're equal, then it moves on to the second elements, etc. – Tom Karzes Sep 15 '20 at 23:23
  • I’m voting to close this question because it is best answered by the language documentation rather than on Stack Overflow. Please see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types – Karl Knechtel Sep 15 '20 at 23:27
  • @Karl How is that a valid close reason? You or someone else could write an answer citing the documentation. **Edit**: actually, [daveldito](https://stackoverflow.com/a/63911127/4518341) has done just that – wjandrea Sep 15 '20 at 23:34
  • Because you are supposed to try to answer the question yourself, and the documentation is the first place to look. Stack Overflow tried a separate project once to create more accessible documentation; it failed. But using a search engine will readily find me the official docs, or - hold on, I tried again, and found a duplicate. – Karl Knechtel Sep 15 '20 at 23:49
  • 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) – rene Sep 16 '20 at 06:37

2 Answers2

2

Python built-in min function uses the 'ordering' rule as per the type of the argument(s) you provide to the min function. This is what @kaya3 suggested in his comment.

Your understanding form the documentation is not complete:

min(iterable, *[, key, default])

min(arg1, arg2, *args[, key])

Return the smallest item in an iterable or the smallest of two or more arguments.

So you can actually pass an iterable OR multipel arguments, well, as long as the arguments can be ordered.

min([1,2,3]) #Outputs 1, passing an iterable
min(1,2,3) #Outputs 1, passing multiple arguments.

Now, if you run this min(1, [1,2,3]), you'll get the following error:

TypeError: '<' not supported between instances of 'list' and 'int'

This means that Python can't 'order', hence can't find the minimum of the two arguments, because your definition of a minimum could be different from mine, for instance, in case of strings.

Comparing two lists (i.e. iterables)

From the documentation:

Sequence objects typically may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

In case when you run min(<list>,...<list>), that's exactly how the comparison is being done. And hence, nothing unexpected.

DaveIdito
  • 1,546
  • 14
  • 31
0

You can use recursion:

code:

def smallest(lst_var):
    small = None
    for i in lst_var:
        if type(i) == list:
            if small != None:
                small = min(small, smallest(i))
            elif small == None:
                small= smallest(i)
        else:
            if small != None:
                small = min(small, i)
            elif small == None:
                small = i
    return small

print(smallest([23, [123,213,12,3], [1232,21,3,123,1], 3412, 123]))

KetZoomer
  • 2,701
  • 3
  • 15
  • 43