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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -