On Tue, Nov 09, 2021 at 04:23:50PM -0800, Hasan Diwan wrote: > As of 3.7. dicts are sorted[1], but I'm unsure if the order can be > overridden.
Dicts are not sorted, they are kept in insertion order. >>> d = {3: 'a', 4: 'b', 1: 'c', 2: 'd'} >>> d {3: 'a', 4: 'b', 1: 'c', 2: 'd'} See the docs: https://docs.python.org/3/library/stdtypes.html#dict although you have to scroll almost all the way to then end, just before the dict view objects, to see it documented. Sorting dicts has been discussed on the Python-Ideas mailing list, it is too hard and expensive to justify for the limited use-cases for it. If you want to sort a dict, you are best to sort the dict's keys, then create a new dict. Or possibly use a dedicated Sorted Mapping type like a red-black tree or similar. Hasan wrote: > >>> print(cmp(Girls, Boys)) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > NameError: name 'cmp' is not defined The 'cmp' built-in was removed in Python 3.0. In any case, dicts don't support order comparisons. -- Steve _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/AV5RVB37YR7TZ3ZURI4ZIHCS5Y2XV67Y/ Code of Conduct: http://python.org/psf/codeofconduct/