python - Reconstructing list from JSON using itertools -
is possible reconstruct list(snippet of result):
[ [ { "counthigh": 3, "brgy_locat": "barangay 2", "municipali": "cabadbaran city" } ], [ { "brgy_locat": "barangay 2", "countmedium": 22, "municipali": "cabadbaran city" } ], [ { "brgy_locat": "barangay 2", "countlow": 13, "municipali": "cabadbaran city" } ] ]
into this?
[ [ { "counthigh": 3, "countmedium": 22, "countlow": 13, "brgy_locat": "barangay 2", "municipali": "cabadbaran city" } ] ]
the list output of query in can't change it. using django.
edit: let's have other dict of same brgy_locat
, municipali
counthigh: 5
should add both of values. reconstructed list should counthigh:8
. result given snippet.
using chain.from_iterable:
>>> itertools import chain >>> result = {} >>> d in chain.from_iterable(a): ... result.update(d) ... >>> result {'municipali': 'cabadbaran city', 'counthigh': 3, 'brgy_locat': 'barangay 2', 'countmedium': 22, 'countlow': 13}
Comments
Post a Comment