Python Forum

Full Version: Sorting a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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}
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)]