Reputation: 11
I have two lists of equal length.
The first list consists of 1000 sublists with two elements each e.g
listone = [[1,2], [1,3], [2,3],...]
My second list consists of 1000 elements e.g.
secondlist = [1,2,3,...]
I want to use these two lists to make my third list consisting 1000 sublists of three elements. I want to have my list in such a fashion that every index is added from secondlist to listone as the third element e.g.
thirdlist = [[1,2,1], [1,3,2], [2,3,3],...]
Upvotes: 0
Views: 91
Reputation: 25094
You can use the zip function to combine listOne
and second
. To get the desired format you have to create a new list using list comprehension
listOne = [[1,2], [1,3], [2,3]]
secondList = [1,2,3]
thirdList = [x + [y] for x,y in zip(listOne, secondList)]
print thirdList
>>> [[1, 2, 1], [1, 3, 2], [2, 3, 3]]
Upvotes: 1
Reputation: 60143
Look into zip
:
listone = [[1,2], [1,3], [2,3]]
secondlist = [1,2,3]
thirdlist = [x + [y] for x, y in zip(listone, secondlist)]
print(thirdlist)
# Output:
# [[1, 2, 1], [1, 3, 2], [2, 3, 3]]
Upvotes: 4