You can use it, a little long but with clear and simple explained passages, there are thousands different ways of doing it anyway and a lot of "more elegant" ones:
### dictionary in your example
d = { "a":{"d":"val1", "e":"val2", "f":"null"}, "b":{"p":"val3", "q":"val4", "r":"val5" }}
### new dictionary to fill with the results
compared = {}
### open loop on key, value elementd of your dict
for k, v in d.items() :
### open a new dict to store the equals, unequals, nulls
subcompared = {}
### open the lists to store the elements
equals = []; unequals = []; nulls = [];
### open loop on k,v items of the subdictionary in your dict
for sub_k, sub_v in v.items() :
### take the first 3 letters of the value (assuming your comparison what kind of it)
sub_v_start = sub_v[0:2]
### IF null put in list and skip to next iteration
if sub_v == "null" :
nulls.append( sub_k )
continue
### open another loop on the subdictionaries of the dict
for sub_k2, sub_v2 in v.items() :
### if same value of the outer loop skip to net iteration
if sub_k2 == sub_k or sub_v2 == "null" :
continue
else :
### if same first 3 letters of the subvalue
if sub_v2.startswith(sub_v_start) :
### if not in list put the keys in list (a simple way to avoid suplication, you could also use SET after etc.)
if sub_k not in equals :
equals.append( sub_k )
if sub_k2 not in equals :
equals.append( sub_k2 )
### if first 3 letters differents the same for unequals
else :
if sub_k not in unequals :
unequals.append( sub_k )
if sub_k2 not in unequals :
unequals.append( sub_k2 )
### put the lists as values for the relative keys of the subdictionary renewed at every outest loop
subcompared["equals"] = equals
subcompared["unequals"] = unequals
subcompared["nulls"] = nulls
### put the subdictionary as value for the same KEY value of the outest loop in the compared dict
compared[k] = subcompared
### print results
compared
{'b': {'unequals': [], 'equals': ['q', 'r', 'p'], 'nulls': []}, 'a': {'unequals': [], 'equals': ['e', 'd'], 'nulls': ['f']}}
if you want the keys of the new dict ordered in a specific way you can use OrderedDict for example