This answer relies on my answer to your previous question. After getting b
, the inverted dictionary, as explained in my previous answer, you can get your output with this code:
def print_data(b):
for key, value in b.items():
print key
print '-' * len(key)
for k, v in value.items():
print ' {}: {}'.format(k, v),
print '\n'
This gives:
cereal
------
barakobama: 9.21
potatoes
--------
barakobama: 2.67
parsley
-------
vladimirputin: 1.33 barakobama: 0.76
sugar
-----
barakobama: 1.98
crisps
------
barakobama: 1.09
milk
----
vladimirputin: 2.87
bread
-----
vladimirputin: 0.66
Note that the order of food items in your expected output and my answer is not the same. This is because dictionaries are not ordered by default. You might need to explore OrderedDicts if order of elements is a concern.
Documentation on format()