Keys order in dictionaries

Brainwashed vald at valis.amber.eu.org
Thu Jun 26 19:26:16 EDT 2003


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





More information about the Python-list mailing list