4 ms·
No, OrderedDict has it's own C implementation which was created just before it was decided that dict would preserve order across iteration. Further there is a
by zurtex 7y ago
No, OrderedDict has it's own C implementation which was created just before it was decided that dict would preserve order across iteration.
Further there is a big difference, regular dict preserves order across iteration but OrderedDict treats order up to equality.
I.e. this returns True:
{1: 1, 2: 2} == {2: 2, 1: 1}
Where as this returns False:
OrderedDict({1: 1, 2: 2}) == OrderedDict({2: 2, 1: 1})
To make that difference speedy it needs to be done on the C level.
- masklinn 7y agoAlso ordereddicts provide methods to move items to the start or end, and remove items specifically at the start or end, not so for regular dicts.
- xapata 7y agodict has popitem for removing at the end. That used to be arbitrary, but now it (de facto) means last-inserted.
- ehsankia 7y agoFor the former, I realize that's how it's done now, but there's nothing forcing it to stay that way. For the latter issue, as explained above, can't they just implement a replacement __eq__ only for OrderedDict, and still re-use the new dict implementation? Similarly, any subtle difference can be shimmed on top of the new implementation inside Python, no?
- xapata 7y agoDepends what OrderedDict was being used for. If used for some of the obscure ordering features, replacement with dict + a shim might be a slowdown.