Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'subsequent': 0.05; '(so': 0.07; 'element': 0.07; 'none,': 0.07; '[1,': 0.09; 'difference,': 0.09; 'false.': 0.09; 'iterate': 0.09; 'types:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(2,': 0.16; '(3,': 0.16; '[none,': 0.16; 'callable': 0.16; 'fetch': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itemgetter': 0.16; 'python3.': 0.16; 'tuple,': 0.16; 'typeerror:': 0.16; 'sat,': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'sort': 0.25; 'second': 0.26; 'post': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'message- id:@mail.gmail.com': 0.30; 'work.': 0.31; '"",': 0.31; '>>>>': 0.31; 'keys': 0.31; 'values.': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'error.': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'pm,': 0.38; 'that,': 0.38; 'recent': 0.39; 'explain': 0.39; 'though,': 0.39; 'either': 0.39; 'simply': 0.61; "you're": 0.61; 'more': 0.64; 'effectively': 0.66; 'frank': 0.68; 'useful.': 0.68; '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=fJiy6uDFZyX4FVXfAvenRkkcavwUTNF1JcW910bH5zs=; b=EfdmdKwZhDTwpQiunaeMDcreuYunVTMP+dMdrLDTp56AFMlR0VG7xhNDnCzMIu1EMv XavrNMTGdR5rlCuyFyjpdPYslciWP1p+ZEcMvvnlGyM/KAcuqtMVYBnajfGvP3ZoZpQc C9cY1t0Bz4IwNpqiczcUFjg88B8hrUmV81F+3ZsOPJL6c3tdcMxaedMzmDx8yKaZxgjr vGXjaZLSJFr2IvnHSRouFc2yqBso9MNdz6bB7HquoBJ+CU8pUAB7aAHzV2V4Otcdsgz6 vTNLcFrOr4nEWo4lptRRiH9cIjzoM/bUT10To24MH8y3T4bBD6U9d283TRsZOXx6yC04 X8rA== MIME-Version: 1.0 X-Received: by 10.68.182.165 with SMTP id ef5mr14853pbc.169.1391847488209; Sat, 08 Feb 2014 00:18:08 -0800 (PST) In-Reply-To: References: Date: Sat, 8 Feb 2014 19:18:08 +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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391847492 news.xs4all.nl 2856 [2001:888:2000:d::a6]:46158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65642 On Sat, Feb 8, 2014 at 7:03 PM, Frank Millman wrote: > I am using python3. I don't know if that makes a difference, but I cannot > get it to work. > >>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>>> sorted(d.items(), key=d.get) > Traceback (most recent call last): > File "", line 1, in > TypeError: unorderable types: NoneType() < NoneType() >>>> You probably hadn't seen my subsequent post yet, in which I explain what's going on here. In Python 2, "None > None" is simply False. (So is "None < None", incidentally.) Py3 makes that an error. But in all your examples, you're effectively trying to sort the list [None, None, None], which is never going to be useful. What you can do, though, is either sort items using itemgetter to sort by the second element of the tuple, or sort keys using dict.get. Using 3.4.0b2: >>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>> sorted(d.keys(), key=d.get) [1, 3, 2] You don't get the values that way, but you get the keys in their correct order, so you can iterate over that and fetch the corresponding values. Alternatively, this is a bit more verbose, but works on items(): >>> import operator >>> sorted(d.items(), key=operator.itemgetter(1)) [(1, 'abc'), (3, 'pqr'), (2, 'xyz')] operator.itemgetter(1) returns a callable that, when passed some object, returns that_object[1]. ChrisA