Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting a dictionary
#1
I have a dictionary like:
dic = {0: [('key1', 0.061),
  ('key2', 0.034),
  ('key3', 0.046),
  ('key4', 0.0593),
  ('key5', 0.091)],
 1: [('key1', 0.025),
  ('key2', 0.046),
  ('key3', 0.014),
  ('key4', 0.054),
  ('key5', 0.058)]
}
How can I sort this based on the float values of each keys.

This is not working:
for i in sorted(dic.values()):
     print(i)
Yoriz write Sep-13-2022, 05:29 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You cannot sort a dictionary. This harkens back to the days when dictionaries were unordered (like sets), so the "order" of items in a dictionary was indeterminant.

Now dictionaries maintain the order of items. Items added to a dictionary stay in the order they were added. This allows sorting a dictionary by collecting all the items in a list, sorting the list, then rebuilding the dictionary from the sorted list. So while you still cannot "sort" a dictionary, you can create a dictionary with the entries sorted by value.

But I don't think you want to sort your dictionary by value. I think you want to sort the lists that make up the dictionary values.
dic = {
    0: [
        ("key1", 0.061),  # This is a list, not a dictionary
        ("key2", 0.034),
        ("key3", 0.046),
        ("key4", 0.0593),
        ("key5", 0.091),
    ],
    1: [
        ("key1", 0.025),
        ("key2", 0.046),
        ("key3", 0.014),
        ("key4", 0.054),
        ("key5", 0.058),
    ],
}

# Sort the tuples in increasing order by the second field.  Does not sort list in dictionary
for key, value in dic.items():
    print(f"{key}\n{sorted(value, key=lambda x:x[1])}")
Output:
0 [('key2', 0.034), ('key3', 0.046), ('key4', 0.0593), ('key1', 0.061), ('key5', 0.091)] 1 [('key3', 0.014), ('key1', 0.025), ('key2', 0.046), ('key4', 0.054), ('key5', 0.058)]
This just changes the order of the items for printout. It does not change the order of the values in the lists. If you wanted to do that, the code would look like this:
dic = {
    0: [
        ("key1", 0.061),
        ("key2", 0.034),
        ("key3", 0.046),
        ("key4", 0.0593),
        ("key5", 0.091),
    ],
    1: [
        ("key1", 0.025),
        ("key2", 0.046),
        ("key3", 0.014),
        ("key4", 0.054),
        ("key5", 0.058),
    ],
}


for value in dic.values():
    value.sort(key=lambda x: x[1])  # Sort the values in place.  Modifies the dictionary

for key, value in dic.items():
    print(key, value)
Output:
0 [('key2', 0.034), ('key3', 0.046), ('key4', 0.0593), ('key1', 0.061), ('key5', 0.091)] 1 [('key3', 0.014), ('key1', 0.025), ('key2', 0.046), ('key4', 0.054), ('key5', 0.058)]
I should not dismiss the possibility that you want the values in your dictionaries to be dictionaries. This requires sorting the tuples and using them to make a dictionary.
dic = {
    0: [
        ("key1", 0.061),
        ("key2", 0.034),
        ("key3", 0.046),
        ("key4", 0.0593),
        ("key5", 0.091),
    ],
    1: [
        ("key1", 0.025),
        ("key2", 0.046),
        ("key3", 0.014),
        ("key4", 0.054),
        ("key5", 0.058),
    ],
}


for key in dic:
    dic[key] = dict(sorted(dic[key], key=lambda x: x[1]))   # Replaces tuple list with a dictionary

for key, value in dic.items():
    print(key, value)
Output:
0 {'key2': 0.034, 'key3': 0.046, 'key4': 0.0593, 'key1': 0.061, 'key5': 0.091} 1 {'key3': 0.014, 'key1': 0.025, 'key2': 0.046, 'key4': 0.054, 'key5': 0.058}
Reply
#3
The easiest to understand is to create a list, in the order you want to sort. Oops, the following list is in reverse order.

list_to_sort=[('key1', 0.061, 0),
('key2', 0.034, 0),
('key3', 0.046, 0),
('key4', 0.0593, 0),
('key5', 0.091), 0],
('key1', 0.025, 1),
('key2', 0.046, 1),
('key3', 0.014, 0),
('key4', 0.054, 1),
('key5', 0.05, 1)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,081 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

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