Alan Wakke Alan Wakke - 1 year ago 51
Python Question

Returning min in list of lists error

Trying to return the min value in this function, but I keep getting the error "TypeError: my_min() takes 1 positional argument but 3 were given." Advice on what I need to change?

def my_min(xss):
min = xss[0]
for i in xss:
if i < min:
min = i
return min
my_min([2,3],[],[4,1,-5]) #test function I used

Answer Source

As I mentioned in my comment, your first problem is that you were calling your method with three lists when you should be calling it as a list-of-lists. To be more explicit, you should be calling as:


Now, the next problem you are facing is that you are only iterating through the first level of your list. This is incorrect. You have to realize you have a list of lists. So, to stick to the logic in your solution, you need to iterate again inside your for loop to find the minimum value of each list inside your list. Your min_val (I changed the name because it shadows the built-in min)

def my_min(xss):
    min_val = xss[0][0]

    # iterate over the first loop
    for i in xss:
        # i now is each sublist in your list
        for j in i:
            # j is each item in each sublist i 
            if j < min_val:
                min_val = j
    return min_val


result -> -5