Sorting dictionary by datetime value

Chris Angelico rosuav at gmail.com
Sat Feb 8 03:29:09 EST 2014


On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot <ikorot01 at gmail.com> wrote:
>> Try this:
>>
>> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
>> for key in sorted_items:
>>     print my_dict[key], key
>
>
> This code fail.
> sorted_item is a list of tuples. And so iterating the list in the for loop I
> will get a tuple.
> It probably should be:
>
> for key[1] in sorted_items:
>
> Let me try that.
>

Actually, it's a list of keys - notice that I changed my_dict.items()
into my_dict.keys()?

ChrisA



More information about the Python-list mailing list