Ad

Appending First Value Of A List Into The First Value Of Another List; Python

- 1 answer

Working in python 2.7.

I have two lists (simplified to make explanations clearer).

T = [[1,0], [1,0], [0,5], [3, -1]]
B = [[1], [3], [2], [2]]

I would like to append the second list (B) so that the first value of the B list is appended to the first list in the T list and so on.

Ex- I want the output to look like:

T = [[1, 0, 1], [1, 0, 3], [0, 5, 2], [3, -1, 2]]

I've tried playing around with different types of append functions, but I haven't found anything that works. Knowing python, I'm sure there's a way.

Ad

Answer

map(list.extend, T, B)

or, if you prefer an explicit loop

for t, b in zip(T, B):
    t.extend(b)
Ad
source: stackoverflow.com
Ad