Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I sort my dict ?
#1
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]]}
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,311 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Sort a dict in dict cherry_cherry 4 73,136 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Easy way to sort a nested dict Alfalfa 3 5,639 Dec-07-2018, 04:12 PM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020