Python Forum

Full Version: Code understanding: Need help in understanding dictionary code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Below code is working fine to sum the price for each items. But I could not understand how values-Keys are filled in ordered dict - d.

from collections import OrderedDict
n=int(input())
d=OrderedDict()
for i in range(n):
    item,space,price=input().rpartition(' ')
    price=int(price)
    print("test",d.keys(),d.values())
    if item in d.keys():
        d[item]+=price
    else:
        d[item]=price
for i in d.keys():
    print(i ,d[i])