Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:help': 0.07; 'way:': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'python.': 0.11; 'wed,': 0.15; "'a',": 0.16; "'b',": 0.16; "'d',": 0.16; "'e',": 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pprint': 0.16; 'wrote:': 0.16; 'example.': 0.18; '>>>': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'am,': 0.23; '2015': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'order.': 0.27; 'message-id:@mail.gmail.com': 0.28; 'dictionary': 0.29; 'function': 0.30; 'usually': 0.33; 'received:google.com': 0.34; 'could': 0.35; 'display': 0.37; 'subject:: ': 0.37; 'doing': 0.38; 'easily': 0.39; 'data': 0.40; 'chrisa': 0.84; 'subject:this': 0.85; 'to:none': 0.90 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=V8hKAAuUnOGNG0wcTjDJydRkGqF5wsvCnFNkPqwt3oc=; b=gteJnLnXhAeD6MXCmdEDnZO+7CiNF+xXCm0KySwbXh4jgSwTjAhh3wu31JS6xEyZLu w7eF0HJimgmGemOTM27PVdzg8U9uCxiF0Rl+NDZWK7WoaOpZl3E9NHLiZxT42N9GWqY8 nQIxx8WfsJZCtEr4zb2Z+iRGz5FWzsyareP4l7GrDhK06JEXM0Dmaf8O4bwsfe4latIg HdBLrTr16ayoeR6xyBxt3qpV/zRXSj5FPvtHVBdESmK42npiaElR5ZdB51dra/QSY2La KwnlwuziCxO1b9DBp61sMddhKxJ94I/SfUa0MRFK/OVV1GDqdjxLnKV63dDTut0ueJpo LBcQ== MIME-Version: 1.0 X-Received: by 10.107.16.149 with SMTP id 21mr36387252ioq.53.1433289696244; Tue, 02 Jun 2015 17:01:36 -0700 (PDT) In-Reply-To: <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com> References: <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com> Date: Wed, 3 Jun 2015 10:01:36 +1000 Subject: Re: Please help on this sorted function 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.20+ 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433289704 news.xs4all.nl 2840 [2001:888:2000:d::a6]:42869 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91914 On Wed, Jun 3, 2015 at 6:25 AM, fl wrote: > I am still new to Python. How to get the sorted dictionary output: > > {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'} Since dictionaries don't actually have any sort of order to them, the best thing to do is usually to simply display it in order. And there's a very handy function for doing that: a pretty-printer. >>> import pprint >>> pprint.pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'}) {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'} This one comes with Python, so you can use it as easily as that above example. Or you could do it this way: >>> from pprint import pprint >>> pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'}) {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'} For a lot of Python data structures, this will give you a tidy and human-readable display. ChrisA