Find Maximum and minimum value in a matrix, Python
Question: How can I find the lowest value given the number of the exercise?, I have this
code:

    
    
    mat = []
    calificaciones = []
    #Captures student ID
    def lmat (numeroest):
        mattotal = []
        for i in range (0, numeroest):
            matricula = int(raw_input('Student ID : '))
            mattotal.append (matricula)
        return (mattotal)
    
    
    #Captures grades
    def numest (numeroest):
        mattotal = []
        calif = []
        for i in range (0, numeroest):
            numcal = input ('Introduce the ammount of grades: ') 
            for j in range (0, numcal):
                matricula = int(input('Input the grades: '))
                calif.append (matricula)
            mattotal.append (calif)
        return (mattotal)
    

So if a user inputs the number of the exercise it will output the lowest grade
in said exercise (e.g., exercise 2, that means it will be the lowest)

    
    
    def givelowest ():
    row = input ('Enter the number of the exercise: ')
        for ...
    

I want to make a for loop that looks for that row (number of exercise, then
gives the lowest number in said row.


Answer: To get the `min/max` of a column you can use a function with
`operator.itemgetter`:

    
    
    from operator import itemgetter
    
    def min_or_max(m, col, f):   
        return f(map(itemgetter(col), m))
    

Then call it passing the matrix, what column and the func to use i.e min or
max:

    
    
    In [22]: m = [[9, 2, 8],[4,6,8],  [3, 1, 2]]
    
    In [23]: func(m, 2, max) 
    Out[23]: 8
    
    In [24]: func(m, 2, min)
    Out[24]: 2
    

Or using indexing in a gen exp:

    
    
    def func(m, col,  f):
        return f(row[col] for row in m)
    

If you want both in a single iteration:

    
    
    from operator import itemgetter
    
    
    def func(m, col):
        mn, mx = float("inf"), float("-inf")
        for i in map(itemgetter(col), m):
            if mn > i:
                mn = i
            if mx < i:
                mx = i
        return mn, mx
    
    
    m = [[9, 2, 8], [4, 6, 8], [3, 1, 2]]
    
    mn, mx = func(m, 2)
    

