Split dictionary of lists into list of dictionaries

前端 未结 8 1830
时光取名叫无心
时光取名叫无心 2020-12-09 21:16

What i need to do is to convert something like this

{\'key1\': [1, 2, 3], \'key2\': [4, 5, 6]}

into

[{\'key1\': 1, \'key2\'         


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-09 21:34

    How about?

    d = {'key1': [1, 2, 3], 'key2': [4, 5, 6]}
    [dict(zip(d.keys(),i)) for i in zip(*d.values())]
    

    Returns:

    [{'key1': 1, 'key2': 4}, {'key1': 2, 'key2': 5}, {'key1': 3, 'key2': 6}]
    

提交回复
热议问题