I have a dictionary like this:
{'11': 6, '10': 3, '15': 2, '14': 1, '04': 3, '16': 4, '19': 1, '18': 1, '09': 2, '17': 2, '06': 1, '07': 1}
and I want to sort the dictionary based on the key and produce something like:
('04', 3), ('06', 1), ('07', 1), ('09', 2), ('10', 3), ('11', 6), ('14', 1), ('15', 2), ('16', 4), ('17', 2), ('18', 1), ('19', 1)
I tried hours = list(dict.items()) and it works pretty well, but when I tried earlier
for hour, freq in dict:
count = (hour, freq)
lst.append(count)
lst.sort()
print lst
I get
('0', '4'), ('0', '6'), ('0', '7'), ('0', '9'), ('1', '0'), ('1', '1'), ('1', '4'), ('1', '5'), ('1', '6'), ('1', '7'), ('1', '8'), ('1', '9')
It seems like only the first digit of the hours are recorded, but I don't know why. The for loop worked well when I was counting frequency of a character in a given string. Can somebody please help me explain this? Thanks a lot.