Skip to content Skip to sidebar Skip to footer

Replacing Values In List Of List Python

I have a list of lists containing random values, for instance [[1,2,3], [2,3,1], [3,2,3]] I would like to go through the list, and replace every type of value, lets say 1, with

Solution 1:

Use a list-comprehension:

lst = [[1,2,3], [2,3,1], [3,2,3]]

print([['a' if y == 1 else 'b' for y in x] for x in lst])
# [['a', 'b', 'b'], ['b', 'b', 'a'], ['b', 'b', 'b']]

Post a Comment for "Replacing Values In List Of List Python"