<div dir="ltr"><div>I think that your mistake is defining OrderedDict as a dict sorting by insertion order. I see no reason to define it that way, and the fact that insertion order is the default is not a reason in my opinion. It's just a dict with an order, and I see no reason to not let users move elements about as they wish. Yes, I'm aware that the documentation defined <span style="font-size: 13px;">OrderedDict</span><span style="font-size: 13px;"> your way too; I still think it's a pointless restriction. </span></div><div><br></div><div>Regarding examples:</div><div><br></div>I've used my `OrderedDict.sort` at least 10 times. Just today I've used it again. I was putting three items in an ordrered dict, with keys 'low', 'medium' and 'high'. I wanted to have them sorted as 'low', 'medium' and 'high' but the insertion order was different because of the algorithm that calculated them. (Also not all 3 items were guaranteed to exist, I wanted to sort those that existed.)<div><br></div><div>So I created an OrderedDict of my subclass and called `.sort`.</div><div><br></div><div>I'm sure you can think of a bunch more examples, if not I can give them to you.<br><br>On Tuesday, September 24, 2013 3:13:15 PM UTC+3, Steven D'Aprano wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Tue, Sep 24, 2013 at 04:49:20AM -0700, Ram Rachum wrote:
<br>> What do you think about providing an `OrderedDict.sort` method? I've been 
<br>> using my own `OrderedDict` subclass that defines `sort` for years, and I 
<br>> always wondered why the stdlib one doesn't provide `sort`.
<br>> 
<br>> I can write the patch if needed.
<br>
<br>I'm not entirely sure why anyone would need an OrderedDict sort method. 
<br>Ordered Dicts store keys by insertion order. Sorting the keys goes 
<br>against the purpose of an OrderedDict.
<br>
<br>I can understand a request for a SortedDict, that keeps the keys in 
<br>sorted order as they are deleted or inserted. I personally don't have 
<br>any need for one, since when I need the keys in sorted order I just 
<br>sort them on the fly:
<br>
<br>for key in sorted(dict):
<br>    ...
<br>
<br> 
<br>but in any case, that's a separate issue from sorting an OrderedDict. 
<br>Can you explain the use-case for why somebody might want to throw away 
<br>the insertion order and replace with sorted order?
<br>
<br>
<br>
<br>-- 
<br>Steven
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="qjIcUlYR53UJ">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br></blockquote></div></div>