Use a nested list comprehension with conditional
>>> l = [[1,None],[2,4],[1.5,2]] >>> def findMax(j): ... return max(i[j] for i in l) ... >>> [[j if j is not None else findMax(k) for k,j in enumerate(i)] for i in l] [[1, 4], [2, 4], [1.5, 2]]
Here, list comprehension checks if every element is None or not. If not, it will print the number, otherwise it will be the maximum and print this element.
Another way to use map is
>>> l = [[1,None],[2,4],[1.5,2]] >>> maxVal = max(map(max, l)) >>> [[j if j is not None else maxVal for k,j in enumerate(i)] for i in l] [[1, 4], [2, 4], [1.5, 2]]
source share