Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.075 X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'cc:addr:python-list': 0.11; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'igor': 0.16; 'iterating': 0.16; 'simplified': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'purposes': 0.26; 'this:': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'code': 0.31; 'keys': 0.31; 'could': 0.34; 'charset:us-ascii': 0.36; 'list': 0.37; 'pm,': 0.38; 'changed': 0.39; 'to:addr:gmail.com': 0.65; 'default': 0.69; 'behavior': 0.77; '7:25': 0.84; 'actually,': 0.84; 'dict,': 0.84; 'fail.': 0.84; 'received:50.22': 0.84 Date: Sat, 8 Feb 2014 08:11:53 -0600 From: Tim Chase To: Chris Angelico Subject: Re: Sorting dictionary by datetime value In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391868679 news.xs4all.nl 2915 [2001:888:2000:d::a6]:37639 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65680 On 2014-02-08 19:29, Chris Angelico wrote: > On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot > 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