I am having the following lists:
listA = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
listB = [1,2,3,4]
and I want:
listC = [[1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 1, 3], [1, 1, 1, 1, 4]]
I am using the following code:
for i in range(len(listA)):
listA[i].append(listB[i])
The result is ok but I want to do that in one line using list comprehension(if possible, or another more elegant way). I can understand a simple list comprehension but not more complicated.
This should do the trick:
[x + [y] for x, y in zip(listA, listB)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With