lundi 11 juillet 2016

Python update dictionary into dictionary


I need to build a dictionary and update it in a for loop. loc = {} result = { I get the list of records from database here, which has id and status } for res in result: temp={} temp['id']= res.id temp['status'] = res.status loc.update(temp) # this replaces the values every time, I want append print loc below is the result expected by me: loc = { {'id' : 123 , 'status' : 'available' }, { 'id' : 456 , 'status' : 'unavailable'} } Can someone help me. I tried with : loc = [] # [] this works with loc.append(temp) this gives result as : [{'id' : 123 , 'status' : 'available'},{'id' : 456 , 'status' : 'unavailable'}] But I need the above result with dictionary of dictionaries in python.

Aucun commentaire:

Enregistrer un commentaire