Skip to content
Advertisement

how to merge similar observations by key attributes in json python

Im working with JSON in python, It new to me, and im trying to merge similar observations by key attributes. The JSON looks like this:

JavaScript

I tried to create a list with unique observations using this:

JavaScript

the output is:

JavaScript

then I tried to look for that list values and to add to them the observations but for some reason, it’s only giving me back the latest one:

JavaScript

Advertisement

Answer

Looks like you need a groupby.

Using itertools.groupby. Note this works only if the list is ordered.

Ex:

JavaScript

Output:

JavaScript

If the list is unordered use a simple dict

Ex:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement