What I am doing here is taking data from a ReportRequest object ("response") that contain data from Google Analytics. My intention it to take the data in this object, and stick it into a dictionary where each key is a list of values:
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader',
{}).get('metricHeaderEntries', [])
rows = report.get('data', {}).get('rows', [])
metrics_ls = [str(metricHeaders[x]['name'][3:]) for x,y in enumerate(metricHeaders)]
DataDict = dict.fromkeys(metrics_ls, [])
# stick each piece of data in appropriate place of DataDict
for cur_row in rows:
cur_vals = cur_row.get('metrics', [])[0]
for i, cur_metric in enumerate(metrics_ls):
DataDict[cur_metric].append( float(cur_vals['values'][i]) )
In the above, I initialize DataDict with a set of keys that contain empty lists, []. I then append values to the appropriate key of the list in a for loop. However, in the end, all of my keys contain the same list, which is just a giant list of all the values. In other words, every key of DataDict contains the same set of numbers, but what I am intending if for subsets of all of these numbers to go into the appropriate key of my dict, so that the dict contains several keys each with a different list of numbers.