Alan Wakke - 6 months ago 39

Python Question

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

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:

```
my_min([[2,3],[],[4,1,-5]])
```

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
print(my_min([[2,3],[],[4,1,-5]]))
```

result -> `-5`