Cleaning up conditionals

Chris Angelico rosuav at gmail.com
Tue Jan 3 04:58:08 EST 2017


On Tue, Jan 3, 2017 at 8:18 PM, Deborah Swanson
<python at deborahswanson.net> wrote:
> OrderedDict is the new development I'd read about, but I think the key
> order is just the order keys were added to the dict. The nice thing
> about that though is when you loop over a dict you will always get the
> key/value pairs in the same order, where with the standard dict they
> come out in arbitrary order. Very disconcerting if you're watching the
> loop as it progresses over muiltiple executions. I imagine you could
> sort an OrderedDict so the keys originally added randomly would be put
> into some kind of good order.

If you want a "SortedDict", you can get that fairly easily just by
iterating over sorted(d) rather than simply d. You can also subclass
the dictionary and change how it iterates, if you like.

ChrisA


More information about the Python-list mailing list