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:
tempads=[ ['doron-2020-026', ['19.0.1']], ['doron-2020-026', ['18.0.6']], ['doron-2020-023', ['19.0.0']], ['doron-2020-023', ['18.0.5']], ['doron-2020-023', ['17.0.7']] ]
I tried to create a list with unique observations using this:
for tempad in tempads: ads.append(tempad[0]) ads = list(dict.fromkeys(ads))
the output is:
['doron-2020-026', 'doron-2020-023']
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:
for ad in ads: for i in range(0,len(tempads)-1): if ad==tempads[i][0]: ad=[ad,tempads[i][1]] newads.append(ad) print(newads) [ ['doron-2020-026', ['19.0.1']], ['doron-2020-023', ['19.0.0']] ]
Advertisement
Answer
Looks like you need a groupby.
Using itertools.groupby
. Note this works only if the list is ordered.
Ex:
from itertools import groupby tempads=[ ['doron-2020-026', ['19.0.1']], ['doron-2020-026', ['18.0.6']], ['doron-2020-023', ['19.0.0']], ['doron-2020-023', ['18.0.5']], ['doron-2020-023', ['17.0.7']] ] result = [[k, [n for _, i in v for n in i]] for k, v in groupby(tempads, lambda x: x[0])] print(result)
Output:
[['doron-2020-026', ['19.0.1', '18.0.6']], ['doron-2020-023', ['19.0.0', '18.0.5', '17.0.7']]]
If the list is unordered use a simple dict
Ex:
result = {} for k, v in tempads: result.setdefault(k, []).extend(v)
Output:
{'doron-2020-023': ['19.0.0', '18.0.5', '17.0.7'], 'doron-2020-026': ['19.0.1', '18.0.6']}