For example, there is a list:

lst = [[1, 0], [0, 0, 2]] 

How to replace all zeros to become:

 lst = [[1, 1], [1, 1, 2]] 
  • one
    eval (str (lst) .replace ('0', '1')) - vadim vaduxa

1 answer 1

 lst = [[1,0],[0,0,2]] newLst = [[1 if j == 0 else j for j in i] for i in lst] print(newLst) 

Will return

 [[1, 1], [1, 1, 2]]