I have three lists of lists, and I’m trying to write a generator function to help me package up values in the same index.
So my lists:
list1 = [[1, 2, 3], [2, 3, 4],...]
list2 = [[4, 5, 6], [5, 6, 7],...]
list3 = [[8, 9, 10], [9, 10, 11],...]
My desired output:
result1 = [[1, 4, 8], [2, 5, 9],...]
result2 = [[2, 5, 9], [3, 6, 10],...]
result3 = [[3, 6, 10], [4, 7, 11],...]
My attempt:
def bundle(x, y, z, index):
for row in x, y, z:
for item in row[index]:
yield list(item)
I keep getting float is not iterable errors. If i modify it slightly:
def bundle(x, y, z, index):
for row in x, y, z:
for item in row:
yield item[index]
I get the values I want as one large sequence, but I would prefer to keep them grouped in a nested style.
If you’re dealing with large lists, a custom, fully lazy approach would be the following: