python - Dictionary and list value processing -


i have code produce dict follows:

defaultdict(<type 'list'>, {u'b0116ce25cad4106becbbddfffa61a1c': [u'demo_ins1', u'2530.0'], u'1578f81703ec4bbaa1d548532c922ab9': [u'new_ins_1', u'1750.0', u'new_tenant_ins', u'2520.0']}) 

code:

# inilialize necessary dict dict_compute = defaultdict(list)  tenants,instances in tenant_id_dict.iteritems():          single_ins in instances:                 compute_value_for_instance = ck.reports.get_total(tenant_id=tenants, service='compute', instance_id=single_ins)                 dict_compute[tenants].append(single_ins)                 dict_compute[tenants].append(compute_value_for_instance)                 print dict_compute 

dictionary combination of tenant_id, instance_name , cost.

i need keep relation between each other.

i need result should follows:

{u'b0116ce25cad4106becbbddfffa61a1c': [u'demo_ins1' : u'2530.0'] 

ie: each tenant should displaying instance name(demo) , cost(2530.0) needed.

need better way same, 1 best list or dict.

i should able fetch , process if needed.

i ok anything(list or dict) need keep relation.

someone have , let me know solution or suggestions.

better try initializing list , appending dictionary below-

from collections import defaultdict # inilialize necessary dict dict_compute = defaultdict(list)  tenants,instances in tenant_id_dict.iteritems():     single_ins in instances:         compute_value_for_instance = ck.reports.get_total(tenant_id=tenants, service='compute', instance_id=single_ins)         dict_compute[tenants].append({single_ins:compute_value_for_instance})         print dict_compute 

edit- if need oneliner below work no need of using defaultdict-

dict_compute = {tenants:[{single_ins:ck.reports.get_total(tenant_id=tenants, service='compute', instance_id=single_ins)} single_ins in instances] tenants,instances in tenant_id_dict.iteritems()} 

Comments