-2

I d like to ask you how to sort dictionary by keys...the task is:you have a string of words and you have to count how much words are there and how much times is there ...for example : Hi mom hi dad hi -> hi 3 mom 1 dad 1 ...this is where I ended with this code :

zoznam = {}
x= 1
string= input ("")
rozdel= string.split()
for i in range (len(rozdel)):
    x = rozdel.count(rozdel[i])    
    zoznam[rozdel[i]] = x
print zoznam

..but now I have to sort that dictionary by keys (words) by first letter ..from example before -> dad 1 hi 3 mom 1 ...thank you :)

sliziky
  • 129
  • 2
  • 3
  • 10

1 Answers1

2

You can use sorted():

for key in sorted(zoznam):
    print key, zoznam[key]
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • -0 because answer lacks a disclaimer about dictionaries being unordered. This is a newbie style question. It needs an answer targeted to that level of understanding. – Steven Rumbalski Nov 17 '14 at 15:15