Roberto Bonvallet: > What output are you expecting from your example matrix? If you are expecting > it to be 5 (the smallest positive element), using "min" is the way to do it: > >>> matrix = [[9, 8, 12, 15], > ... [0, 11, 15, 18], > ... [0, 0, 10, 13], > ... [0, 0, 0, 5]] > >>> min(min(x for x in row if x > 0) for row in matrix) > 5
This looks a bit like homework... To find the minimal number > 0 of the matrix this seems better: mat = [[9, 8, 12, 15], [0, 11, 15, 18], [0, 0, 10, 13], [0, 0, 0, 5]] print min(el for row in mat for el in row if el > 0) Note that this raises TypeError is the matrix doesn't have one or more numbers > 0 If the OP needs the position he/she can do something like: nozeromin = 1e300 # or something similar pos = None, None for nrow, row in enumerate(mat): for ncol, el in enumerate(row): if el > 0 and el < nozeromin: nozeromin = el pos = nrow, ncol print nozeromin, pos Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list