Q:

Comparing 2 dictionaries in python and create a list with values from dictionary

I have 2 dictionaries.
dict1={'Name':'','Age':'','Salary':''}
dict2={'Name':'John','Salary':'25k'}

Required output is a list where if the key is matching the matching key's value will be added to the list. If not matching, a space has to be populated to the list. For example, Age is not available in dict2 and hence the 2nd item in list should be space and the output order should not change. 
[John, ,25k]

So far i tried using a for loop to find matching keys, but not sure how to populate spaces for non-matching values and to maintain the order in dict1.
Any help would be highly appreciated. Thanks in advance

A:

You could just use a list comprehension to iterate the keys from dict1 and get the corresponding value from dict2 or " " as a default.
>>> dict1={'Name':'','Age':'','Salary':''}
>>> dict2={'Name':'John','Salary':'25k'}
>>> [dict2.get(key, " ") for key in dict1]
['John', '25k', ' ']

Note: Using Python 3.5 here, so dict1's order gets messed up. Using OrderedDict:
>>> dict1 = collections.OrderedDict([('Name',''),('Age',''),('Salary','')])
>>> [dict2.get(key, " ") for key in dict1]
['John', ' ', '25k']

