[Python-Dev] More compact dictionaries with faster iteration

Tim Delaney tim.delaney at aptare.com
Mon Dec 10 22:29:50 CET 2012


On 11 December 2012 05:01, Armin Rigo <arigo at tunes.org> wrote:

> Hi Philip,
>
> On Mon, Dec 10, 2012 at 5:16 PM, PJ Eby <pje at telecommunity.com> wrote:
> > On the other hand, this would also make a fast ordered dictionary
> > subclass possible, just by not using the free list for additions,
> > combined with periodic compaction before adds or after deletes.
>
> Technically, I could see Python switching to ordered dictionaries
> everywhere.  Raymond's insight suddenly makes it easy for CPython and
> PyPy, and at least Jython could use the LinkedHashMap class (although
> this would need checking with Jython guys).  I'd vaguely argue that
> dictionary orders are one of the few non-reproducible factors in a
> Python program, so it might be a good thing.  But only vaguely ---
> maybe I got far too used to random orders over time...
>

Whilst I think Python should not move to ordered dictionaries everywhere, I
would say there is an argument (no pun intended) for making **kwargs a
dictionary that maintains insertion order *if there are no deletions*. It
sounds like we could get that for free with this implementation, although
from another post IronPython might not have something suitable.

I think there are real advantages to doing so - a trivial one being the
ability to easily initialise an ordered dictionary from another ordered
dictionary.

I could also see an argument for having this property for all dicts. There
are many dictionaries that are never deleted from (probably most dict
literals) and it would be nice to have an iteration order for them that
matched the source code.

However if deletions occur all bets would be off. If you need to maintain
insertion order in the face of deletions, use an explicit ordereddict.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20121211/a1d0aff2/attachment.html>


More information about the Python-Dev mailing list