Regarding Dictionaries in python

Anton Vredegoor anton at vredegoor.doge.nl
Wed Apr 23 08:02:41 EDT 2003


"Mehta, Anish" <Anish.Mehta at enst-bretagne.fr> wrote:

> >>>D= {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4}
> >>>D.items()
>[ ('b', 2), ('c', 3), ('d', 4), ('a', 1)]
>
>
>why it is not showing the items in order in which i have inserted. 
>Please tell me why it is happing like this.

Others have already explained this phenomenon. I would like to add
that the language designers do not guarantee that dictionaries will
always return their items in the same order across implementations.

So, for example on another Python interpreter:

Python 2.2.1 (#1, Jun 25 2002, 10:55:46) 
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> D= {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4}
>>> D.items()
[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
>>> 

This gives the language designers enough flexibility to always provide
us with "state of the art" implementations of the dictionary type.

At the same time this makes the dictionary type the place where Python
hides most of its ambiguity.

Anton









More information about the Python-list mailing list