![]() |
| How can I sort my dict ? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I sort my dict ? (/thread-16166.html) |
How can I sort my dict ? - Mike Ru - Feb-16-2019 I have it {1: ['3', ['third task', True]], 2: ['1', ['first task', True]], 3: ['2', ['second task', True]]}I need to get sort dict like that{1: ['1', ['first task', True]], 2: ['2', ['second task', True]], 3: ['3', ['third task', True]]} RE: How can I sort my dict ? - ichabod801 - Feb-16-2019 Since Python 3.7, dictionaries are guaranteed to be ordered by insertion order. Before that, you would need to use collections.OrderedDict. Once you have a dictionary ordered by insertion order, you sort it by getting the keys, sorting the keys, and then reinserting the key/value pairs in that order. |