Reputation: 37
I have following dictionary:-
dictionaty = {
'specs': {
'classq0': {'priority': 0, 'BwShare': '10%'},
'classq1': {'priority': 1, 'BwShare': '30%'},
'classq2': {'priority': 2, 'BwShare': '60%'}
}
}
I need to access values of priority and BwShare for each class i.e I need output like following:-
prio_classq0 = 0
BwShare_classq0 = 10%
prio_classq1 = 1
BwShare_classq1 = 30%
prio_classq2 = 2
BwShare_classq2 = 60%
Thanks a lot.
Upvotes: 0
Views: 38
Reputation: 287
If you don't need output to be in exact same order:
for key, value in dictionaty['specs'].items():
for key2, value2 in value.items():
first = key2 if key2!='priority' else 'prio'
print(first + '_'+ key + ' = ' + str(value2))
Upvotes: 0
Reputation: 1228
Try this:
dictionary = {
'specs': {
'classq0': {'priority': 0, 'BwShare': '10%'},
'classq1': {'priority': 1, 'BwShare': '30%'},
'classq2': {'priority': 2, 'BwShare': '60%'}
}
}
out_dictionary = {}
for outer_key, inner_dict in dictionary['specs'].items():
for inner_key, value in inner_dict.items():
if inner_key == "priority":
inner_key = "prio"
out_dictionary = {
**out_dictionary,
f"{inner_key}_{outer_key}": value
}
print(out_dictionary)
Upvotes: 1