I have Python code accessing data in a database (in Django):
for result in request.user.results.all():
for word in result.word.all():
data.append(dict(zip([str(word.type.type)], [str(word.word)])))
which displays ‘data’ as such:
[{'color': 'blue'}, {'kind': 'pencil'}, {'rating': 'high'}, {'color': 'red'}, {'kind': 'truck'}, {'rating': 'low'} and so on....
I want to rewrite the python code to display this instead:
[{'color': 'blue', 'kind': 'pencil', 'rating': 'high'}, {'color': 'red', 'kind': 'truck', 'rating': 'low'} and so on....
Are these two the same or does the location of the ‘{‘ matter? I wrote the code above but I’m not how (or if I need to) modify it.
It looks to me like your zipping together single-item lists to make single item dicts. I think what you want instead is:
zipis typically used to make dicts in situations when you have two pre-existing lists, not so much when you’re constructing a dict from scratch from objects.