Sorting dictionary by datetime value

Tim Chase python.list at tim.thechases.com
Sat Feb 8 09:11:53 EST 2014


On 2014-02-08 19:29, Chris Angelico wrote:
> 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)
> >
> > This code fail.
> 
> Actually, it's a list of keys - notice that I changed
> my_dict.items() into my_dict.keys()?

Unless you need to make a copy (for purposes of altering the original
while iterating), that's the default behavior of iterating over a
dict, so it could be simplified to

  sorted_items = sorted(my_dict, key=my_dict.get)

-tkc





More information about the Python-list mailing list