I realize that the title of this question might not be immediately super clear, so on an example:
My dict contains lists as follows (assume there's nothing else in it):
packed = {
'names': ['a', 'b', 'c', 'd'],
'values': ['A', 'B', 'C', 'D']
}
What I want to do is to iterate over each of these lists (as many as there might be) in parallel, but so that during each iteration I have a dict with a single element of each list. I imagine something like this:
for pack in smartIterate(packed): #how to "smartIterate"?
print(pack['names'], pack['values'])
#pack is equal to {'names':'a', 'values':'A'} in the first iteration
printing out that:
a, A
b, B
c, C
d, D
I realize this can be done with explicitly iterating over the length of one sublist and constructing a dict during each iteration:
for i in range(len(packed.values()[0])):
pack = dict(zip(packed.keys(),[v[i] for v in packed.values()]))
but it feels like there is a cleaner, more efficient - pythonic - way to do that.
Similar questions: