How do I sort values in a nested dict-format?

Viewed 47

I currently have a dictionary with key: string, and values is another dict.

How do I sort by the values in the inner dictionary? (I want to sort it by the number, 14, 8, 2, etc)

Example dictionary data:

{
    "00": {
        "057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json": 14,
        "067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json": 2,
        "0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json": 8,

    },
    "000": {
        "02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json": 1,
        "02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json": 1,
        "03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json": 2,
    }
}

Expected Output:

After sorting by the number, it prints it from largest number to least (format does not have to be like this, but you get the point):

    "057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json": 14,
    "0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json": 8,
    "067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json": 2,

My attempt:

When the key matches "00" it will print out all the values in that key : value pair.

for key, value in data.items()
    for k, v in sorted(value.items(), lambda x: x[1]):
        if key == "00":
           print(key)
1 Answers

Like this:

In [298]: my_dict = {}

In [299]: for key,value in data.items(): 
     ...:     my_dict[key] = {k: v for k, v in sorted(value.items(), key=lambda item: item[1], reverse=True)} 
     ...:                                                                                                                                                                                                   

In [300]: my_dict                                                                                                                                                                                           
Out[300]: 
{'00': {'057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json': 14,
  '0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json': 8,
  '067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json': 2},
 '000': {'03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json': 2,
  '02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json': 1,
  '02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json': 1}}

OR, you can use a nested dict comprehension:

In [305]: {key: {k: v for k, v in sorted(value.items(), key=lambda item: item[1], reverse=True)} for key,value in data.items()}                                                                             
Out[305]: 
{'00': {'057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json': 14,
  '0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json': 8,
  '067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json': 2},
 '000': {'03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json': 2,
  '02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json': 1,
  '02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json': 1}}
Related