Reputation: 2935
I have two list one with the label and the other data. for example
label = ["first","second"] list = [[1,2],[11,22]]
I need the result to be a list of dictionary
[ { "first" : 1, "second" : 2, }, { "first" : 11, "second" : 22, } ]
Is there a simple way to do that. Note the label and list might vary, but number of entry remain the same.
Upvotes: 0
Views: 1554
Reputation: 28598
Try this:
>>> [dict(zip(label, e)) for e in list]
[{'second': 2, 'first': 1}, {'second': 22, 'first': 11}]
Upvotes: 1
Reputation: 95298
>>> label = ["first","second"]
>>> lists = [[1,2],[11,22]]
>>> [dict(zip(label, l)) for l in lists]
[{'second': 2, 'first': 1}, {'second': 22, 'first': 11}]
Upvotes: 7