Turning a list of dictionaries into a list of lists

前端 未结 4 1125
难免孤独
难免孤独 2021-01-20 22:55

I know this is possible with list comprehension but I can\'t seem to figure it out. Currently I have a list of dictionaries like so:

 [ {\'field1\': \'a\', \'fie         


        
4条回答
  •  逝去的感伤
    2021-01-20 23:41

    You can try:

    [[x['field2'], x['field1']] for x in l]
    

    where l is your input list. The result for your data would be:

    [['b', 'a'], ['d', 'c'], ['f', 'e']]
    

    This way you ensure that the value for field2 comes before the value for field1

提交回复
热议问题