Is it possible to sort a dictionary?

Carlos Alberto Reis Ribeiro cribeiro at mail.inet.com.br
Mon Mar 26 21:37:29 EST 2001


At 03:09 27/03/01 +0200, you wrote:
>Alex Martelli <aleaxit at yahoo.com> wrote:
>
> > Fine!  Even faster & simpler:
>
>Simpler yes, but faster? Are you sure?  Lately I tested something
>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:

- it would use less memory (no need to store the items sequence elsewhere). 
Python don't copy all the data, only the references, but even this can be 
big for a large dictionary.

- it would run faster for most cases, and possibly *much* faster if the 
loop have a "break" for some condition. The startup time would also be 
greatly reduced for a large dictionary.

As part of my recent work with adapters (as discussed on the list), I was 
thinking about implementing a 'proxy' object to take the dictionary and 
make it behave like a sequence of items. For the xitems() version to work, 
I need to find a way to walk sequentially through the hash table directly, 
and I haven't figured out to make it - I was far away from the computer 
when the idea ocurred to me. If someone know I would be glad to learn <wink>


Carlos Ribeiro






More information about the Python-list mailing list