Collections.OrderedDict()

  • + 0 comments
    • from collections import OrderedDict
    • d = OrderedDict()
    • for _ in range(int(input())):
    • item, quantity = input().rsplit(None, 1)
    • d[item] = d.get(item, 0) + int(quantity)
    • for item, quantity in d.items():
    • print(item, quantity)

    use rplit instead of rpartition. this way you don't need to save space