We know in Python 3.6 dictionaries are insertion ordered as an implementation detail, and in 3.7 insertion ordering can be relied upon.
I expected this to also be th
Counter
and defaultdict
are both ordered now, and you can rely on it. Counter
just doesn't look ordered because its repr
was designed before dict ordering was guaranteed, and Counter.__repr__ sorts entries by descending order of value.
def __repr__(self):
if not self:
return '%s()' % self.__class__.__name__
try:
items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
return '%s({%s})' % (self.__class__.__name__, items)
except TypeError:
# handle case where values are not orderable
return '{0}({1!r})'.format(self.__class__.__name__, dict(self))