Is there a one line code to find maximal value in a matrix?

Viewed 5132

To find the maximal value in a matrix of numbers, we can code 5 lines to solve the problem:

ans = matrix[0][0]
for x in range(len(matrix)):
    for y in range(len(matrix[0])):
        ans = max(ans, matrix[x][y])
return ans

Is there a one line solution for this problem? The one that I came up with is pretty awkward actually:

return max(max(matrix, key=max))

or

return max(map(max, matrix))
4 Answers
Related