Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder7.xlned.com!news2.euro.net!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:object': 0.07; 'python': 0.08; 'object?': 0.09; 'wrote:': 0.15; 'called,': 0.16; 'dictionaries': 0.16; 'generated,': 0.16; 'lambda': 0.16; 'received:192.168.1.40': 0.16; 'subject:sort': 0.16; 'tmp': 0.16; 'subject:list': 0.16; 'convert': 0.19; 'header:In-Reply-To:1': 0.22; 'default,': 0.23; 'code': 0.24; 'changed': 0.25; 'all,': 0.28; 'hash': 0.30; 'class': 0.31; 'url:library': 0.31; 'subject:?': 0.31; 'print': 0.32; 'source': 0.32; 'list': 0.32; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'do?': 0.35; 'subject:new': 0.36; 'url:python': 0.37; 'received:192': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'perhaps': 0.39; 'received:192.168.1': 0.39; 'should': 0.39; 'url:docs': 0.39; 'to:addr:python.org': 0.39; 'order': 0.62; 'increase': 0.64; 'received:62': 0.67; 'order,': 0.73; 'from:addr:t': 0.84 Date: Tue, 02 Aug 2011 20:12:49 +0200 From: Thomas Jollans User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20110628 Thunderbird/5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: how to sort a hash list without generating a new object? References: In-Reply-To: X-Enigmail-Version: 1.2 OpenPGP: id=5C8691ED Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 1312308759 news.xs4all.nl 23955 [2001:888:2000:d::a6]:59901 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10756 On 02/08/11 20:02, smith jack wrote: > the source code is as follows > > x={} > x['a'] = 11 > x['c'] = 19 > x['b'] = 13 > print x > > tmp = sorted(x.items(), key = lambda x:x[0]) # increase order by > default, if i want to have a descending order, what should i do? > # after sorted is called, a list will be generated, and the hash list > x is not changed at all, how to convert x to a sorted hash list > without generating a new object? > print tmp > print x Python dictionaries are never ordered. Perhaps the collections.OrderedDict class can do what you're looking for. http://docs.python.org/py3k/library/collections.html#collections.OrderedDict