def compare(a, b):
    print('compare', a, b)
d = OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
for key1 in d:
    for key2 in reversed(d):
        if key1 == key2:
            break
compare(key1, key2)
Efficient comparison of all elements of python dict