Collections.OrderedDict()

  • + 0 comments

    I know ordered dictionaries are the norm in modern versions of py3, but added the import just for the spirit of things.

    from collections import OrderedDict as od
    
    if __name__ == '__main__':
        n = int(input())
        dic = od()
        for i in range(n):
            product, price = input().rsplit(' ', 1)
            dic[product] = int(price) + dic.get(product, 0)
        print(*(f"{k} {v}" for k, v in dic.items()), sep='\n')