We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Collections.OrderedDict()
Collections.OrderedDict()
Sort by
recency
|
695 Discussions
|
Please Login in order to post a comment
from collections import OrderedDict n = int(input()) item = OrderedDict()
for _ in range(n): item_name,price = input().rsplit(' ',1) price = int(price)
for item_name,item_price in item.items(): print(f'{item_name} {item_price}')
def col_net(): n = int(input()) dic = {} lis = [] for _ in range(n): line = input().strip() item,price = line.rsplit(' ',1) price = int(price)
col_net()
One of the great features of OrderedDict is that even if you update an existing key with a new value, it doesn't lose its original position in the order. https://bcgame.ind.in/contact/
Since CPython 3.6, normal
dict
keep the insertion order. Since Python 3.7, it is a guaranteed language feature. So you can just check the version of Python and use a normaldict
instead of an OrderedDict (or better here, adefaultdict
) and it will work with all implementations of Python 3.7+. It works on Hackerrank for this exercise, both with adict
and adefaultdict
.here's my code: