Reputation: 13135
I have a function that returns a list via yield. I use this function as follows:
myList = []
for i in range(10):
myList = myList + list(myListGenerator(i))
pickleFile = open("mystuff.dat", "wb")
pickle.dump(myList, pickleFile)
pickleFile.close()
I'm just wondering if this is the most efficient way to pickle the data or if I can combine the generators (myListGenerator(0), myListGenerator(1), etc) into one generator which can then be used by pickle.
Sorry if my question sonds daft but I'm new to both generators and pickle... Thanks,
Barry
Upvotes: 2
Views: 564
Reputation: 176800
You can combine the results of the generators (created using a generator expression) into a single list with itertools.chain.from_iterable
:
pickle.dump(list(itertools.chain.from_iterable(
myListGenerator(i) for i in range(10))), pickleFile)
Or rewrite the generator to include the range
call internally, and then just do
pickle.dump(list(myListGenerator(10)), pickleFile)
Upvotes: 7