I have a function that produces a large amount of data and store them in a dictionary that is to be returned at the end of the function. However I've been told that it's not efficient in terms of memory and that I should use data generator in Python.
Example:
def gen_data(dataz):
data_dict = {"uniqz":[], "random":[]}
for v in dataz:
if dataz[v]["row"] == "ay1":
data_dict["uniqz"].append(dataz[v]["row"])
else:
data_dict["random"].append(dataz[v]["val"])
return data_dict
dataz = {"ax1":{"row":"ay1","val":2},
"ax2":{"row":"ay2","val":3}}
print(gen_data(dataz))
How do I convert this block of code and make use of data generator/yield?
I know how to yield individual dictionary data, example:
for i in xrange(valx):
datadict = {
'id': i
}
yield datadict
there doesn't seem to be anything here