-4

I do have a dict of ditcs; This needs to get sorted in a way FAIL group appears first in the dict followed by pass (even if there is one "FAIL" condition they should be grouped together).

To add more details if I were to sort in alphabetical order of the values I would do like

sorted_d1=collections.OrderedDict(d1.items(),lambda x:x[1])

but if I were to find for "FAIL" string in the values and group them, how am I supposed to do that? Kindly help on this

d1 = 
{
    'test1':{'condition1':'PASS','condition2':'FAIL','condition3':'-'},
    'test2':{'condition1':'PASS','condition2':'PASS','condition3':'-'},
    'test3':{'condition1':'-'   ,'condition2':'PASS','condition3':'-'},
    'test4':{'condition1':'PASS','condition2':'-'   ,'condition3':'-'},
    'test5':{'condition1':'-'   ,'condition2':'FAIL','condition3':'-'},
    'test6':{'condition1':'FAIL','condition2':'PASS','condition3':'-'},
    'test7':{'condition1':'FAIL','condition2':'-'   ,'condition3':'-'}
}

sorted_d1 =  
{  
    'test1':{'condition1':'PASS','condition2':'**FAIL**','condition3':'-'},
    'test5':{'condition1':'-'   ,'condition2':'**FAIL**','condition3':'-'},
    'test6':{'condition1':'**FAIL**','condition2':'PASS','condition3':'-'},
    'test7':{'condition1':'**FAIL**','condition2':'-'   ,'condition3':'-'},
    'test2':{'condition1':'PASS','condition2':'PASS','condition3':'-'},
    'test3':{'condition1':'-'   ,'condition2':'PASS','condition3':'-'},
    'test4':{'condition1':'PASS','condition2':'-'   ,'condition3':'-'}
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • `dicts` are unordered in `python2.7` – Sohaib Farooqi Mar 07 '18 at 09:47
  • actually till python3.6 its unordered. In 3.7 its ordered – Arpit Solanki Mar 07 '18 at 09:48
  • Any references for ordered dict in 3.7? Otherwise there is a [OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict) – hellow Mar 07 '18 at 09:51
  • Possible duplicate of [How do I sort a list of dictionaries by values of the dictionary in Python?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python) – hellow Mar 07 '18 at 09:51
  • In a hurry I have badly presented the question .. Extremely sorry for that.. if I were to sort in alphabetical order of the values I would do like sorted_d1=collections.OrderedDict(d1.items(),lambda x:x[1]) but if I were to grep for "FAIL" in the values and order it, how am I supposed to do that? – Venkatanathan Ramamurthi Mar 07 '18 at 09:54
  • Hi @hellow. Thanks for the inputs. I feel there is one more level of granularity needed where this is sorting by a "particular value" than the generic sorting by values – Venkatanathan Ramamurthi Mar 07 '18 at 10:00

1 Answers1

0

I'm not a big fan of ordering dicts, so here's a list of keys:

result = []
for key, my_dict in d1.items():
    if 'FAIL' in my_dict.values():
        result.insert(0, key)
    else:
        result.append(key)

print(result)

The keys that contain 'FAIL' come first. Other than that, there's no particular order. If you need to differentiate between 'FAIL and not 'FAIL' and keep the original order (which is signified by the key, I assume), you could make two lists, one with 'FAIL' and one without and just sort those.

uwain12345
  • 356
  • 2
  • 21
  • cool thanks a lot. This was the thing I was looking for "'FAIL' in my_dict.values()". Since I'm a newbie in this I don't have the clarity in expressing my requirements. sorry for the obscurity in the problem statement @uwain12345 – Venkatanathan Ramamurthi Mar 08 '18 at 07:02