Iterating order of dict.items() 
Author Message
 Iterating order of dict.items()

In a script I am writing, when I iterated over the items in a dict
using:

for x,y in mydict.items():
    doSomethingWith(x,y)

The order the items comes out in appears to be undefined.  I would
have thought that without any other sorting that the items would just
come out in the order that the dict was defined in, so:

mydict = {"one": 1, "two": 2, "three": 3, "four": 4}

would yield values:

x     | y
------+--
one   | 1
two   | 2
three | 3
four  | 4

However the order appears to be random say three, four, one, two for
example

So, is the order of dict.items() defined?  If so, what is that order?
Is there any way to control the order my dict is iterated over?

Thanks a lot.  

Cheers,

Robin



Sat, 07 Jan 2006 23:06:28 GMT  
 Iterating order of dict.items()

    Robin> So, is the order of dict.items() defined?  If so, what is
    Robin> that order?  Is there any way to control the order my dict
    Robin> is iterated over?

See the FAQ:
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.091.htp

The Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747

And the usenet archives on http://groups.google.com
  search for: python order dictionary

John Hunter



Sun, 08 Jan 2006 00:04:41 GMT  
 Iterating order of dict.items()

Quote:

> [snip]

> So, is the order of dict.items() defined?  If so, what is that order?
> Is there any way to control the order my dict is iterated over?

   The order is implementation dependent.  It may change across different
versions of Python, and should not be depended upon.  There is no way to
control this in the built-in dictionary type, but it is possible to subclass
dict and override any of keys/values/items/iterkeys/itervalues/iteritems you
desire to acheive the appropriate ordering.

  Jp

--
"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler



Sun, 08 Jan 2006 00:03:17 GMT  
 Iterating order of dict.items()


Quote:
> The order the items comes out in appears to be undefined.

Correct.

Quote:
> I would have thought that without any other sorting that the items
> would just come out in the order that the dict was defined in,

No, the whole point of hashing keys is to pseudorandomly spread them
more or less uniformly around the storage array with as little
dependence on input order as possible.  This is what make insertion
and deletion close to O(1) rather than O(n) operations.

Terry J. Reedy



Sun, 08 Jan 2006 06:11:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Iterating over the data items in a dict.

2. NewBuiltins: added dict() -- inverse of dict.items()

3. Iterating over items in the registry

4. need dict to maintain order

5. expanding a list and dict to a *args and **dict

6. Dict can't update with dict-like objects

7. Builtin dict should be callable, since a dict defines a funct ion

8. Builtin dict should be callable, since a dict defines a function

9. Dict to String and String to Dict with Visual Works

10. for items in a list, in order, do

11. Deleting Serial Numbered Order Item From Invoice

12. Order of items in cluster

 

 
Powered by phpBB® Forum Software