Collections.OrderedDict()

Sort by

recency

|

720 Discussions

|

  • + 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())

  • + 1 comment
    from collections import OrderedDict
    
    n = int(input())
    item_list = OrderedDict()
    for _ in range(n):
        item = input().split()
        key = ' '.join(item[:-1])
        value = int(item[-1])
        item_list[key] = item_list.get(key,0)+value
    for k,v in item_list.items():
        print(k,v)