I have two lists:
l1 = ['a','b','c','d','e','f','g','h', ...]
l2 = ['dict1','dict2','dict3','dict4','dict5','dict6','dict7','dict8', ...]
I need to run a function on a chunk of each list 50 items at a time, and it can't continue until the function has returned the result of the first 50 items in each list.
My first idea was using a generator:
def list1_split(l1):
n = 50
for i in range(0, len(l1), n):
yield l1[i:i+n]
def list2_split(l2):
n = 50
for i in range(0, len(l2), n):
yield l2[i:i+n]
chunk_l1 = list1_split(l1)
chunk_l2 = list1_split(l1)
Then when using both lists I place them in the main function:
def query(chunk_l1, chunk_l2):
count = 0
query_return_dict = {}
for i, j in zip(chunk_l2, chunk_l1):
count += 1
query_return_dict[i] = j
print('This is number ', count, '\n', j)
return query_return_dict
def main():
thread = threading.Thread(target=query(chunk_l1, chunk_l2))
thread.start()
print('Done!')
if __name__ == '__main__':
main()
My first error that I get, is unrelated to the generators (I think):
TypeError: 'dict' object is not callable
But what really threw me off was when I used the debugger my for loop was interpreting each list as:
i: <class 'list'>: ['a','b','c','d','e',...]
j: <class 'list'>: ['dict1','dict2','dict3','dict4',...]
Instead of i: 'a', j: 'dict1'
, on top of that I get an error saying,
TypeError: unhashable type: 'list'
I'm not too familiar with generators but it seems the most useful for running functions a chunk at a time