Home > other >  getting a list of dictionaries as a list of lists
getting a list of dictionaries as a list of lists

Time:12-01

Ok so I have a list of the same dictionaries and I want to get the values of the dictionaries into a list of lists. For example this is what one dictionary might look like:

mylist = [{'a': 0, 'b': 2},{'a':1, 'b':3}]

I want the lists of lists to look like:

[[0,2],[1,3]]

I have tried doing

zip(*[d.values() for d in mylist])

however this results in a list of different keys for example:

[[0,1],[2,3]]

CodePudding user response:

As the comments suggest, I don't think you need zip() for this to work, instead just try something simpler such as [list(i.values()) for i in mylist]
You convert the values into a list with the list() function, and the values are already obtained with the .values() method

CodePudding user response:

Try this [list(i.values()) for i in mylist]

  • Related