Is it possible to sort a dictionary?

Carel Fellinger cfelling at iae.nl
Tue Mar 27 09:10:26 EST 2001


Carlos Alberto Reis Ribeiro <cribeiro at mail.inet.com.br> wrote:
> At 03:09 27/03/01 +0200, you wrote:
...
>>similar and was surprised when I found the dict.items function to be
>>soo slow that going through the whole dict indexing each entry
>>one by one was faster.

> I was thinking about something similar today. It would be useful to have a 
> "xitems()" method, behaving exactly like items(), but *without* the need to 
> build the tuple in advance. I see two advantages:

I don't know.  One can only sort after the whole list exists, so a
non-sorting xitems() won't suffice.  In this case we wanted it to be
sorted on keys, in other cases one would like it sorted on values.
Are you proposing all kind of flavours for the xitem() method?

I just wonder why the items() method is slooow; values() and keys()
are both much faster, going over the whole dict indexing from keys()
is even faster.  Is tuple making/unpacking so expensive?  Or is a dict
optimized for key/value access on the expense of items()?  I know I
should dive into the python-code, but maybe someone can save me a few
minutes:)


-- 
groetjes, carel



More information about the Python-list mailing list