Is numeric keys of Python's dictionary automatically sorted?

Ant antroy at gmail.com
Thu Mar 8 16:26:46 EST 2007


On Mar 8, 8:20 am, Steven D'Aprano <s... at REMOVEME.cybersource.com.au>
wrote:
...
> And the problem with a dictionary is that some people want to make sense
> of its order, just like in this case, and the fifty thousand previous
> times people have asked this newsgroup how they can sort a dictionary.
...
> What makes you think they won't do the same with sets?
...
> (1) Don't assume unordered data structures like sets and dicts are
> ordered, even if they look like it. They aren't.
>
> (2) If you want the keys of a dict sorted, get the keys, and sort them
> when and as you need them.

All good points. Is this misconception really as common as you suggest
(obviously there aren't really going to be 50,000 previous threads of
this nature - but you know what I mean). Because if they are, it is
perhaps a case for adding optimized sorted_dict and sorted_set to the
collections module, similar to the TreeSet and TreeMap classes in
Java.




More information about the Python-list mailing list