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
You can combine the results of the generators (created using a generator expression) into a single list with
itertools.chain.from_iterable:Or rewrite the generator to include the
rangecall internally, and then just do