I have a list of values that I want to add its elements to the end of each list in a list of lists. Is there a Pythonic or efficient way to solve this?
For example, given:
x = [['a','b','c'],['d','e','f'],['g','h','i']]
y = [1,2,3]
I would expect:
[['a', 'b', 'c', 1], ['d', 'e', 'f', 2], ['g', 'h', 'i', 3]]
I've tried:
list(zip(x,y))
But, this produces:
[(['a', 'b', 'c'], 1), (['d', 'e', 'f'], 2), (['g', 'h', 'i'], 3)]
I can solve it with an inefficient loop like this:
new_data = []
for i,x in enumerate(x):
x.append(y[i])
new_data.append(x)
print(new_data)
[['a', 'b', 'c', 1], ['d', 'e', 'f', 2], ['g', 'h', 'i', 3]]