Collections.OrderedDict()

  • + 1 comment

    You can use simple unpacking for the item name like this:

    from collections import OrderedDict
    od = OrderedDict()
    for i in range(int(input())):
      *item, price = input().split()
      item = ' '.join(item)
      od.setdefault(item, 0)
      od[item] = od[item] + int(price)
    for k,v in od.items():
      print(k + ' ' + str(v))