getting a list of dictionaries as a list of lists

Question:

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]]

Asked By: JaredCusick

||

Answers:

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

Answered By: ViktorGlushak

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

Answered By: realhuman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.