Keys order in dictionaries

B

Brainwashed

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
 
B

Ben Finney

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.
 
D

Duncan Booth

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top