I have a number of outputs (collected from db queries) and mapped into a dictionaries with key:value pairs db_column: value
Sample data that i want to combine:
dicts1 = [
{'K1': 'kval', 'L1': 'Lval', 'F1': None, 'F2': None, 'F3': 'ERR1'},
{'K1': 'kval', 'L1': 'Lval', 'F1': None, 'F2': None, 'F3': 'ERR1'},
{'F1': None, 'F2': None, 'F3': 'ERR2'}]
Now i would like to combine those dictionaries into single one but values from F1
, F2
, F3
shall be concatenated with ;
if not None
. If it's None
then leve value as it is.
Ideally: if some key exists in special_key=('F1', 'F2', 'F3')
then concatenate..
result_dict: {'K1': 'kval', 'L1': 'Lval', 'F1': None, 'F2': None, 'F3': 'ERR1;ERR2;ERR3'}
on the base of question asked in: Python - Combine two dictionaries, concatenate string values? i figured out some piece of code and get stucked
def concat_dict(*dicts):
keys = set().union(*dicts)
print "keys: ", keys
outdict = {}
for k in keys:
print "key: ", k
for dic in dicts:
print k, dic.get(k)
outdict[k] = dic.get(k)
Any help appreciated.