The question is not completely defined, and the accepted answer will fail for some dictionaries. He relies on ordering keys, which is not guaranteed. Adding additional keys to the dictionary, deleting keys, or even adding an order can affect the order.
A safer solution is to select one dictionary, d in this case, to get the keys, and then use them to access the second dictionary:
d = {'a':5, 'b':6, 'c': 3} d2 = {'a':6, 'b':7, 'c': 3} [(k, d2[k], v) for k, v in d.items()]
Result:
[('b', 7, 6), ('a', 6, 5), ('c', 3, 3)]
This is no more complicated than the other answers, and it is clear which keys are available. If the dictionaries have different key orders, say d2 = {'x': 3, 'b':7, 'c': 3, 'a':9} , you will still get consistent results.
source share