[Solved] Replace elements in list of lists with defined value – Python [closed]


You can use a nested list comprehension to generate a new list keeping the nested structure, using if/else statements to populate it with * or a space accordingly:

L = [[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [1, 1, 1, 1, 1]]

[['*' if i else ' ' for i in l] for l in L]

[[' ', ' ', '*', ' ', ' '],
 [' ', '*', '*', '*', ' '],
 ['*', '*', '*', '*', '*']]

solved Replace elements in list of lists with defined value – Python [closed]