-1

I've a nested dictionary DictA as below

Dict A  
{
'a': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4,v5'}, 
'b': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}
}

Nested Dictionaries 'a' and 'b' are almost same except the value of 'k4'. How do I sort DictA based on the above difference in value's length.

I want the Dict A to be sorted as below,

Dict A = 
{
'b': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'},
'a': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4,v5'}
}

Please advise.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Praveen
  • 19
  • 5
  • 1
    Not again... apologies, but you can't sort dictionaries. With python3.6, you can create new dictionaries with sorted order, but you still can't sort them. – cs95 Mar 15 '18 at 00:47
  • 2
    Possible duplicate of [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Patrick Haugh Mar 15 '18 at 00:59
  • Yes, I'm trying to copy the dictionary to a new temp one to do operations – Praveen Mar 15 '18 at 01:11

1 Answers1

-1

Use key with your call to sort. In this argument, pass in a lambda to key to sort by a specific value. So:

sorted_dict = sorted (DictA, key = lambda inner_dict: len (inner_dict ["k4"]))
Levi Lesches
  • 1,508
  • 1
  • 13
  • 23
  • Thanks. But I've a huge nested dictionary, I'm struggling with the syntax of iterating through nested dictionaries values by length to sort it. – Praveen Mar 15 '18 at 00:56
  • What version of Python are you using that has `dict`s with a `sort` method? – Patrick Haugh Mar 15 '18 at 00:58
  • Can you elaborate a bit more? Is the dictionary always the same deep? Etc. – Levi Lesches Mar 15 '18 at 00:59
  • SORRY meant `sorted()`, I'll fix it now, care to re-upvote? – Levi Lesches Mar 15 '18 at 00:59
  • Python version 3.6 – Praveen Mar 15 '18 at 01:05
  • No, he was talking to me, I incorrectly called `.sort()` instead of `sorted()` and he was reminding me of that. – Levi Lesches Mar 15 '18 at 01:06
  • The length of nested dictionary varies....For eg: Dict A has 4 nested dictionaries with varied number of nested values. and Dict B might have 2 nested dictionaries with varied number of nested values. – Praveen Mar 15 '18 at 01:07
  • Ok, and where is the one with `"k4"`, or the equivalent of it – Levi Lesches Mar 15 '18 at 01:08
  • I didn't get your question. If you see my question DictA has two nested dicts 'a' and 'b' and the only difference between them is the length of values. I need to identify the nested dict which has minimum value length. – Praveen Mar 15 '18 at 01:14
  • Yes but you said there could be multiple **layers** of dicts inside your main one. I need to know if the value you want to check will always be inside a certain layer or if it will move around. – Levi Lesches Mar 15 '18 at 01:16
  • It will move around definitely. – Praveen Mar 15 '18 at 01:18
  • OK...are there any other keys like it (even in the other layers)? I want to know if I can search for the key in all the layers of the main dict. If not, please post all (or at least most) of your code so I can look for alternative methods – Levi Lesches Mar 15 '18 at 01:19
  • Or if you could describe to me a "pattern" or "rule" that dictates where the value we're looking for will be. – Levi Lesches Mar 15 '18 at 01:25
  • I'm not looking for any pattern. All I'm looking for is the length of the values of a nested key. Dict A { 'a': {'k1': 'v1', 'k2': 'v2', 'k3': '1,2,3,4', 'k4': '1,2,3'}, 'b': {'k1': 'v1', 'k2': 'v2', 'k3': '1', 'k4': '1,2,3'}, 'c': {'k1': 'v1', 'k2': 'v2', 'k3': '1,2', 'k4': '1,2,3'} } – Praveen Mar 15 '18 at 01:28
  • From the above Dict A, I want to insert into another dictionary say Dict B with sorted order b,c,a instead of a,b,c in DictA – Praveen Mar 15 '18 at 01:29
  • Yah so my answer still works. Unless of course, there will be dictionaries inside -- for example -- `a`. Then we need to get a little more complicated. And I edited my answer to give the **length** of the `"k4"` value, not just the value itself. – Levi Lesches Mar 15 '18 at 01:31
  • In any case, can you just post your code as an edit to your answer? It will give me all the context I need. Edit: In response to your edit above, dictionaries CANNOT be sorted. I **can** make you a list of key, value pairs, if you'd like... – Levi Lesches Mar 15 '18 at 01:33
  • I won't be able to specify K4 directly, it could be any value. I need to iterate the dictionary items (all values -maybe using some loop function to know the length and then sort. – Praveen Mar 15 '18 at 01:35
  • OK, just post the rest of your code pls and Ill see what I can do – Levi Lesches Mar 15 '18 at 01:36
  • Thanks for your help!. But, the code is all messed up and I'm just trying to rewrite from scratch. As specified in the question, I'm trying to iterate each dictionary which has multiple nested dictionaries and try to sort them and insert into a new dictionary (using ordered) so that it remains in the same order. – Praveen Mar 15 '18 at 01:42
  • Again, dictionaries have no order. It may look like they do, but they really don't and can shuffle anytime. So please, don't ask for a sorted dictionary. Like I said, I can make you a list of lists (key value pairs). – Levi Lesches Mar 15 '18 at 01:44