Python OrderedDict() function of Collections module

Python OrderedDict() function of Collections module

Instructor-svgAl-Mamun Sarkar
Mar 26 , 2020

Python OrderedDict() function of Collections module. The following code shows how to solve the HackerRank Collections.OrderedDict() Python collections problem. OrderedDict remembers the order of the dictionary keys that were inserted first. 

Code:

from collections import OrderedDict

d = OrderedDict()

for _ in range(int(input())):
    item, space, quantity = input().rpartition(' ')
    d[item] = d.get(item, 0) + int(quantity)

for item, quantity in d.items():
    print(item, quantity)

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram