Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'cc:addr:python-list': 0.11; 'be:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'igor': 0.16; 'iterating': 0.16; 'tuple.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; 'keys': 0.31; 'probably': 0.32; 'received:google.com': 0.35; 'should': 0.36; 'list': 0.37; 'pm,': 0.38; 'changed': 0.39; '7:25': 0.84; 'actually,': 0.84; 'fail.': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=pA1/31WxgLEyQeHn8qNtIncG4gC6LcmBvcnfLtvCZoM=; b=drWLjf3mod/U0m4FXkzrS5QrQyfNfpp5p7H71Jpyd2cDsAnVwKeDyHTfh4GfBhMXcn DlvrKa+PR8c+W5FkJho/PPtP0w7kg1ur/BGchE0N5rRAbvk7UpRnBL25XIV7ubMpCguZ kLHdQL73uybR1mWCeLb6Ml+QfTa+wB8AtDZXwtP+uNPiPCOIL+/PxdsnmZ8K+LYcXn4r bj1ZktmvMeXE3uc+3glIHVKceodAXpgdZWOxw5bW8WjG8a/zUT5M/ZYR5Z5BRbsbuMau cwec6b77pVznBfeAWqsHqsMBowBpOfPsHj12fazCKhwizIlpG4RVD0MNX98+RE1EGIRs tyKA== MIME-Version: 1.0 X-Received: by 10.66.129.133 with SMTP id nw5mr12909539pab.98.1391848149194; Sat, 08 Feb 2014 00:29:09 -0800 (PST) In-Reply-To: References: Date: Sat, 8 Feb 2014 19:29:09 +1100 Subject: Re: Sorting dictionary by datetime value From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391848158 news.xs4all.nl 2922 [2001:888:2000:d::a6]:60118 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65647 On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot 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