Skip to content
Advertisement

Convert a list of objects to a dictionary of lists

I have a list of JSON objects, already sorted (by time let’s say). Each JSON object has type and status. For example:

JavaScript

I’d like to convert it to:

JavaScript

So of course it’s an easy task but I’m looking for the Pythonic way doing that, so it will spare me a few lines of code

Advertisement

Answer

I don’t know if there is a one-liner but you can make use of setdefault or defaultdict to achieve the desired result as:

JavaScript

Using setdefault():

JavaScript

Output:

JavaScript

Using defaultdict:

JavaScript

Output:

JavaScript
Advertisement