Collections.OrderedDict()

  • + 0 comments
    order_d={} #item dict with there price
    for i in range(int(input())):
        item=list(input().split()) #list which contains the item_name and Price but here price in string format at index -1
        price=int(item[-1]) #item price at index -1 in list
        item.remove(item[-1]) #here we remove the price from the list to get item full name , because list contains the item name either space or nor space
        item_name=(" ".join(item)) # here we join the item name with space if input item name is separated with space for example "BANANA FRIES 12", here we join this "BANANA FRIES" with space , after the removing there price from the list
    
        if(item_name in order_d):
            order_d[item_name]=order_d[item_name]+price # in this loop where we find our item is present on dict or not , if present we simply add there price in exiesting item entry.
        else:
            order_d[item_name]=price # in this line if item not present in list , add this item .....
    
    for j in order_d:
        print(j,order_d[j]) # here we print the item name and there price