dictionary - Get the value of particular key from all dictionaries Python -
i have list of dictionary this,
a = [{'segmentname': 'fico high', 'cohortmonthyear': '01-01-2015', 'drivervalue': '0.921996823602789', 'monthsonbooks': '1', 'cohortquarteryear': 'q1 2015', 'portfolioid': '55dc0e48c95a8a1a2ffb836e', 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '01-01-2015', 'drivervalue': '0.9123400335106416', 'monthsonbooks': '2', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '01-01-2015', 'drivervalue': '0.9024207736261439', 'monthsonbooks': '3', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '01-01-2015', 'drivervalue': '0.889915542915732', 'monthsonbooks': '4', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '01-01-2015', 'drivervalue': '0.8806580068608421', 'monthsonbooks': '5', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '02-01-2015', 'drivervalue': '0.9458702933667549', 'monthsonbooks': '1', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '02-01-2015', 'drivervalue': '0.9335318149106486', 'monthsonbooks': '2', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '02-01-2015', 'drivervalue': '0.9225337221412799', 'monthsonbooks': '3', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '02-01-2015', 'drivervalue': '0.9109689291572624', 'monthsonbooks': '4', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '03-01-2015', 'drivervalue': '0.9092991755453589', 'monthsonbooks': '1', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '03-01-2015', 'drivervalue': '0.8988101317456371', 'monthsonbooks': '2', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '03-01-2015', 'drivervalue': '0.8860809446836048', 'monthsonbooks': '3', 'cohortquarteryear': 'q1 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '04-01-2015', 'drivervalue': '0.9471495415096017', 'monthsonbooks': '1', 'cohortquarteryear': 'q2 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '04-01-2015', 'drivervalue': '0.9364314248533585', 'monthsonbooks': '2', 'cohortquarteryear': 'q2 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}, {'segmentname': 'fico high', 'cohortmonthyear': '05-01-2015', 'drivervalue': '0.9344317261744861', 'monthsonbooks': '1', 'cohortquarteryear': 'q2 2015', 'portfolioid': ('55dc0e48c95a8a1a2ffb836e'), 'drivername': 'active rate'}]
i need iterate on list of dictionaries , drivervalue of dictionary without repeating segmentname , drivername , portfolioid.
this code far,
mm = [] in a: ss = {'drivername': i['drivername'], 'segmentname': i['segmentname'], 'portfolioid': i['portfolioid'], 'drivervalue': []} if all([i['drivername'] == 'drivername' , i['segmentname'] == 'segmentname']): ss['drivervalue'].append(i['drivervalue']) mm.append(ss) print(mm)
the drivervalue empty in list. doing wrong?
sample output:
{ "portfolioid" : ("55d4247b119a612af00eff4b"), "drivername" : "active rate", "sampledata" : [ 30, 24, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ] }
theres more elegant approach this, key value pairs each occurrence of "drivervalue" key in list of dictionaries, , store values in list mm:
mm = [] b in a: key,value in b.items(): if key == "drivervalue": mm.append(value)
just played around , approach is:
p = [d['drivervalue'] d in a]
where p list of values. hope helps.
Comments
Post a Comment