python - Add a new column to the elements of a list -
this question has answer here:
i have list (input_m) containing other lists of numbers. want add new column (zero) each element of list have final list (output_m):
input_m = [[3, 2], [5, 1], [4, 7]] output_m = [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
i have tried list comprehensions, don't output_m in format want. here code:
def add_column(matrix): res = [[item,0] item in matrix] return res output_m = add_column(input_m) output_m = [[[3, 2], 0], [[5, 1], 0], [[4, 7], 0]]
any help? thanks
in list comprehension -
res = [[item,0] item in matrix]
item
list, why getting result - [[[3, 2], 0], [[5, 1], 0], [[4, 7], 0]]
try concatenation instead , example -
def add_column(matrix): res = [item + [0] item in matrix] return res
Comments
Post a Comment