print dictionary keys and values in ordered manner

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



print dictionary keys and values in ordered manner



I have a dictionary with keys and values as shown below.


from datetime import datetime
dic = 'pack1':'stage1','pack2':'stage2','pack3':'stage3','pack4':'stage4'



I want to print the keys and their corresponding values in ordered manner in new line, like



Update at 2018-09-18 09:58:03.263575



The code I tried is given below


print("Update at %snDeliverytttttttState nttt".format(key for key in dic.keys(), val for val in dic.values()) %datetime.now())



But it give the output as



Update at 2018-09-18 09:58:03.263575



The values doesn't correspond to their keys, and the output is given in a single line as a set of lists. How to format the output as my requirement?
(There are whitespaces between 'delivery' and State but stack overflow doesn't show them)





Possible duplicate of Dictionaries: How to keep keys/values in same order as declared?
– Alex Taylor
9 mins ago




3 Answers
3



Dictionary order is not preserved by default. If you want that for any reason then you need to use OrderedDict instead





Note: As of 3.6, CPython 3.6 preserves it as an implementation detail; in 3.7, it's preserved as a language guarantee in all Python interpreters.
– ShadowRanger
9 mins ago



You need to loop over your input, you can't do this cleanly in a single format and print operation.


format


print



If the keys only need to match their values, just do:


dic = 'pack1':'stage1','pack2':'stage2','pack3':'stage3','pack4':'stage4'

for k, v in dic.items(): # .viewitems() on Python 2.7
print("ttt".format(k, v))



If you need the output ordered, not just matched up, dict don't provide that guarantee before 3.7 (and don't provide even implementation dependent guarantees until 3.6). You'd need a collections.OrderedDict. You could do a presort though:


dict


collections.OrderedDict


for k, v in sorted(dic.items()): # .viewitems() on Python 2.7
print("ttt".format(k, v))



so the output ordering is predictable.



It is good to note that since Python 3.7, a dict preserves the order in which data is entered. Prior to that, you can use an OrderedDict.


dict


OrderedDict



Although, it seems what you want is to sort the key-value pairs in alphabetical order of keys. In that case, you can use sorted.


sorted


dic = 'pack1': 'stage1', 'pack2': 'stage2', 'pack3': 'stage3', 'pack4': 'stage4'

for k, v in sorted(dic.items()):
print (k + 't' + v)


pack1 stage1
pack2 stage2
pack3 stage3
pack4 stage4



From there you can work on the printing format.





The OP appears to be primarily using Python 2.7 (the set repr is set([...]), not ...), so you'd need to fix the print to be compatible (or add a __future__ import).
– ShadowRanger
1 min ago


set


set([...])


...


print


__future__





@ShadowRanger there
– Olivier Melançon
1 min ago







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS