dict: keys() and values() order guaranteed to be same?

Dan Stromberg drsalists at gmail.com
Thu Jul 26 23:13:19 CEST 2012

On Thu, Jul 26, 2012 at 9:06 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> Chris Angelico wrote:
>> On Tue, Jul 24, 2012 at 1:20 AM, Steven D'Aprano
>> <steve+comp.lang.python@**pearwood.info<steve%2Bcomp.lang.python at pearwood.info>>
>> wrote:
>>> (Although if you think about the implementation of dicts as hash tables,
>>> it does seem likely that it is trivial to enforce this -- one would have
>>> to work *harder* to break that promise than to keep it.)
>> However, it would be quite reasonable to implement a dict as a splay
>> tree, and have values() return them nearest-first. This would mean
>> that just reading from the dictionary could change the order of
>> values(), yet it wouldn't make the implementation non-conformant.
> Yes, it would.  The docs say that .keys(), .values(), etc., will maintain
> order unless the dict is modified in between calls.
> ~Ethan~
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>

On the other hand, it's a somewhat far-out thing to guarantee. If some
things were relaxed a bit for the benefit of alternative implementations,
I'd think this could be one of the things.  I certainly wouldn't want to
write code that depends on such a behavior, if nothing else because someday
I might want to use a  dict-like-object that doesn't provide such a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120726/6956c7bc/attachment.html>

More information about the Python-list mailing list