Keys order in dictionaries

Discussion in 'Python' started by Brainwashed, Jun 27, 2003.

  1. Brainwashed

    Brainwashed Guest

    Is there any order in dictionaries that will never change ? I've noticed that
    assigning always the same elements to the dict puts them always in the same
    order. Like regexp which takes 3 values, 'year', 'month', 'day' - I always
    get this order:

    'year': .., 'day':.., 'month':..

    No idea why this order tho.. :) Is there any philosophy in this ?
    And if I add only one new value to this regexp, so that it takes 4 now the
    order changes totally, but still remains the same for those 4 particular
    key-names. If I add 'id' to the previous dict I get:

    'year': .., 'id':.., 'day':.., 'month':..

    And every time regexp matches those values order is still the same.
    Can somebody explain this matter or give some urls to pages where I can
    read about this.
    And the second thing that bothers me is assigning multiple values at once
    from dictionary. I mean:

    x, y, z = dict # where dict = { 'xx':.., 'yy':.., 'zz':.. }

    This works okay for me, if I know that dict is always of size 3 and there
    is the same order of keys:values.

    But now I came to the point when this dict can have both - 3 or 4 items
    (same as this example from above, 'id' is the additional item that is not
    always present). And I want to assign x, y, z like I did before, but ommit
    id value. Is it possible to do at once, with some multiple assignment ?
    Maybe there is a way to specify what three items should be taken from dict
    and assigned to x, y, z? I know, I can always do something like:
    x = dict['xx']
    y = dict['yy'] and so on, but I'm just curious if some more complicated
    multiple assignment is possible in Python.

    How would you solve this problem?

    /Vald
     
    Brainwashed, Jun 27, 2003
    #1
    1. Advertising

  2. Brainwashed

    Ben Finney Guest

    On Fri, 27 Jun 2003 01:26:16 +0200, Brainwashed wrote:
    > Is there any order in dictionaries that will never change ?


    No. The specification for the built-in dictionary type specifically
    disclaims any preservation of order, to allow the implementation of any
    arbitrary dictionary algorithm, with different, unpredictable (to the
    user) storage and retrieval orders.

    You can get ordered dictionaries (see the Python cookbook article
    already pointed to), but not by directly using the built-in dictionary
    type.

    --
    \ "I have yet to see any problem, however complicated, which, |
    `\ when you looked at it in the right way, did not become still |
    _o__) more complicated." -- Paul Anderson |
    http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
     
    Ben Finney, Jun 27, 2003
    #2
    1. Advertising

  3. Brainwashed

    Duncan Booth Guest

    Brainwashed <> wrote in
    news::

    > Is there any order in dictionaries that will never change ? I've
    > noticed that assigning always the same elements to the dict puts them
    > always in the same order. Like regexp which takes 3 values, 'year',
    > 'month', 'day' - I always get this order:
    >
    > 'year': .., 'day':.., 'month':..
    >
    > No idea why this order tho.. :) Is there any philosophy in this ?
    >


    Just because you never saw the order change doesn't mean it won't change.
    The code below shows just how easy it is to force the order to change:

    >>> d = { 'year': 1, 'month': 2, 'day': 3 }
    >>> d

    {'month': 2, 'day': 3, 'year': 1}
    >>> for i in range(30): d = i


    >>> for i in range(30): del d


    >>> d

    {'month': 2, 'year': 1, 'day': 3}
    >>> dict(d)

    {'year': 1, 'day': 3, 'month': 2}
    >>>


    (BTW, that last one surprised me, I had expected dict(d) simply to restore
    the original order instead of producing a third ordering).

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Jun 27, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Gerhard_H=E4ring?=

    Re: Keys order in dictionaries

    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 27, 2003, in forum: Python
    Replies:
    0
    Views:
    678
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Jun 27, 2003
  2. W Isaac Carroll

    Re: Keys order in dictionaries

    W Isaac Carroll, Jun 27, 2003, in forum: Python
    Replies:
    0
    Views:
    638
    W Isaac Carroll
    Jun 27, 2003
  3. ProvoWallis
    Replies:
    7
    Views:
    421
    ProvoWallis
    Dec 7, 2005
  4. lysdexia
    Replies:
    6
    Views:
    508
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    492
    Brandon
    Aug 15, 2008
Loading...

Share This Page