0

So I am asked to write a function called find_min that takes a list of lists and returns the minimum value across all lists. No more info was provided. I have been provided the lists, I must test it with the following:

if __name__ == "__main__":
    v = [ [ 11,12,3], [6, 8, 4], [ 17, 2, 18, 14] ]
    print("Min of list v: {}".format(find_min(v)) )
    u = [ [ 'car', 'tailor', 'ball' ], ['dress'], ['can', 'cheese', 'ring' ], \
              [ 'rain', 'snow', 'sun' ] ]
    print("Min of list u: {}".format(find_min(u)) )

This is what I have so far:

def find_min(x):
    for i in x:
     i == 0
     i += 1
     minx = min(x[i])
     if i == 4:
            break
    return minx

I wrote this to try to get through every list. I didn't want to ask without at least a bit of code written. I am new at Python so any help or tips would be greatly appreciated!

  • 1
    If you don't care which list the min value is in, I'd suggest you flatten it then do a min on the flattened list. Here's an answer describing that: https://stackoverflow.com/a/952952/769971 – wholevinski Nov 18 '20 at 14:57
  • 1
    It has already been answered here check it out https://stackoverflow.com/questions/16036913/minimum-of-list-of-lists – DP9 Nov 18 '20 at 15:02
  • 2
    one of your examples was a list of strings list. what do you mean from ```minimum``` in this list? – TheFaultInOurStars Nov 18 '20 at 15:04

2 Answers2

1

You can do it with the following code. It should be noted that the min across a list of strings takes the element which sorts first in alphabetical order. If you for example want to have the shortest string, you would need a different approach

def find_min(lst):
    concat_list = sum(lst, [])  # This line joins all the inner lists into one big list
    return min(concat_list)  # Return the minimum value across the big list
oskros
  • 3,101
  • 2
  • 9
  • 28
0

Try this,

def find_min(x):    
    for i in x:
        v = 0
        for o in x:
            if i <= o:
                v = v + 1
        if v == len(x):
            print()
            print(i)
find_min(x)
Tharu
  • 188
  • 2
  • 14