I'd keep track of the keys that you found and output the ones you didn't find in the end:
dictA = {"apple": 1, "orange": 2, "chocolate": 3, "mango": 4}
dictB = {"man": "abc", "or": "asdf", "app": "fasdfkl"}
found_keys = set()
for key_b in dictB.keys():
for key_a in dictA.keys():
if key_a.startswith(key_b):
print "I can find: %s %s" % (key_a, dictA[key_a])
found_keys.add(key_a)
break
print "I couldn't find: %s" % dict((k, dictA[k]) for k in set(dictA.keys()) - found_keys)
That outputs:
I can find: apple 1
I can find: orange 2
I can find: mango 4
I couldn't find: {'chocolate': 3}
EDIT: I just saw gnibbler's answer. I like it more than mine, although since mine doesn't use any
and explicitly calls the keys()
method of the dict object, it may be a bit easier to understand (but, again, if you get what gnibbler's answer does, go with that one)