Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'a'": 0.07; 'subject:help': 0.07; '[1,': 0.09; 'collections': 0.09; 'items)': 0.09; 'typeerror:': 0.09; 'types:': 0.09; 'example:': 0.11; '(but': 0.15; "'b',": 0.16; "'d',": 0.16; "'e',": 0.16; "('b',": 0.16; "['a',": 0.16; "[('a',": 0.16; 'dictionaries': 0.16; 'dictionary,': 0.16; 'key/value': 0.16; 'remembers': 0.16; 'str()': 0.16; 'unordered': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'odd': 0.18; 'thanks,': 0.19; '>>>': 0.20; '"",': 0.22; 'keys': 0.22; 'tuples': 0.22; 'module': 0.23; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'sort': 0.25; 'header :User-Agent:1': 0.26; 'sequence': 0.27; "doesn't": 0.28; 'dictionary': 0.29; 'sensible': 0.29; 'tutorial': 0.29; 'sense': 0.29; 'values': 0.30; "can't": 0.32; 'traceback': 0.33; 'me?': 0.34; 'file': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'fail': 0.35; 'i.e.': 0.35; 'but': 0.36; 'there': 0.36; 'two': 0.37; 'hi,': 0.37; 'received:10': 0.37; 'subject:: ': 0.37; 'say': 0.38; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'learn': 0.60; 'your': 0.60; 'charset:windows-1252': 0.65; 'dr.': 0.69; 'received:204': 0.75; 'received:10.10': 0.76; 'institute': 0.77; '(425)': 0.84; '895-4418': 0.84; 'digipen': 0.84; 'herron': 0.84; 'subject:this': 0.85 Date: Tue, 02 Jun 2015 14:00:00 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Please help on this sorted function References: <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> In-Reply-To: <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Wed, 03 Jun 2015 09:09:34 +0200 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433315375 news.xs4all.nl 2893 [2001:888:2000:d::a6]:36790 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91922 On 06/02/2015 01:20 PM, fl wrote: > Hi, > > I try to learn sorted(). With the tutorial example: > > > > >>>> ff=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) >>>> ff > [1, 2, 3, 4, 5] > > > > I don't see what sorted does in this dictionary, i.e. the sequence of > 1..5 is unchanged. Could you explain it to me? > > > Thanks, It's best to think of dictionaries as unordered collections of key/value pairs. Dictionaries are not sequences, do not have any particular ordering, and in full generality *can't* be sorted in any sensible way. For instance, this slightly odd (but perfectly legal) dictionary >>> d = {'a':123, 456:'b'} can't be sorted >>> sorted(d) Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int() < str() because it doesn't make sense to order/compare the two keys 'a' and 456. If your dictionary is a little better behaved, say >>> d = {'a':123, 'b':456} you may be able to sort the keys >>> sorted(d) ['a', 'b'] or the values >>> sorted(d.values()) [123, 456] or the key/value tuples (called items) >>> sorted(d.items()) [('a', 123), ('b', 456)] but each of those attempts to sort could fail on a general dictionary if the individual keys or values are not sortable. There is also an implementation of a type of dictionary that remembers the order in which the items are *inserted*. It's in the collections module and called OrderedDict. Gary Herron -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418