Collections.OrderedDict()

Sort by

recency

|

721 Discussions

|

  • + 0 comments
    from collections import OrderedDict,defaultdict
    
    dictionary = defaultdict(int)
    
    N = int(input())
    
    for i in range(N):
        name,price = input().rsplit(" ",1)
        dictionary[name] += (int(price))
        
    order = OrderedDict(dictionary)
    
    for keys in order:
        print(keys,order[keys])
    
  • + 0 comments

    Without any module:

    N = int(input())
    
    item_name= []
    price_list =[]
    
    for _ in range(N):
        *item, price = input().split()
        item_name.append(' '.join(item))
        price_list.append(int(price))
    
    unique_item= []
    unique_price= []
    
    for item, price in zip(item_name, price_list):
        if item not in unique_item:
            unique_item.append(item)
            unique_price.append(price)     
        else:
            ind= unique_item.index(item)
            unique_price[ind]= unique_price[ind]+ price
    
    for item, total_price in zip(unique_item, unique_price):
        print(item, total_price)
    
  • + 0 comments

    IMO: This problem may be helpful to know what is OrderedDict? However Python version 3.10 and beyond already has dictionary as ordered .

    FYI: Used OrderDict from collection or dict() solves the problem

    Do let me know your opinion?

  • + 0 comments

    Here is HackerRank Collections.OrderedDict() in python solution - https://programmingoneonone.com/hackerrank-collections-ordereddict-solution-in-python.html

  • + 0 comments

    This is a bit of a wierd question, implying the computer is input lots of different lines instead of just a single input, for _ in range(int(input())): print (input())