Turning a list of dictionaries into a list of lists

前端 未结 4 1123
难免孤独
难免孤独 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:47

    Just return the dict.values() lists in Python 2, or convert the dictionary view to a list in Python 3:

    [d.values() for d in list_of_dicts]  # Python 2
    [list(d.values()) for d in list_of_dicts]  # Python 3
    

    Note that the values are not going to be in any specific order, because dictionaries are not ordered. If you expected them to be in a given order you'd have to add a sorting step.

提交回复
热议问题