Reputation: 372
I am trying to figure out how to perform the following:
firstDictionary = {"firstKey" : "A", "secondKey" : "B"}
secondDictionary = {"firstKey" : 3, "secondKey" : 47}
emptyDictionary = {}
for key, value in firstDictionary, secondDictionary:
# emptyDictionary = some sort of append method...
so that
secondDictionary = {"A" : 3, "B" : 47}
Upvotes: 2
Views: 3030
Reputation: 63777
This is quite straight forward. All you need to do is to iterate over one of the dictionary items and essentially find the associated value of the second dictionary using the key from the first dictionary.
>>> firstDictionary = {"firstKey" : "A", "secondKey" : "B"}
>>> secondDictionary = {"firstKey" : 3, "secondKey" : 47}
>>> emptyDictionary = {value : secondDictionary.get(key, None) for key, value in firstDictionary.items()}
>>> print emptyDictionary
{'A': 3, 'B': 47}
If you are certain that both the dictionaries have the same key, then replace the dict.get construct with a dictionary indexing
>>> {value : secondDictionary[key] for key, value in firstDictionary.items()}
{'A': 3, 'B': 47}
Upvotes: 1