dictionary - Merge dictionaries from multiple lists in Python -
i have 3 list multiple dictionaries inside.
list1 = [{'question': u'fan offline information can found on screen under general menu? '}, {'question': u'what tool f5 bigip packet traces. '}, {'question': u'on http health monitor configuration. if receive string , disabling string matched , reverse enabled. status of pool members?'}] list2 = [{'answer': u'systeminfoscreen'}, {'answer': u'qkview'}, {'answer': u'offline'}] list3 = [{'correct_answer': u'systeminfoscreen'}, {'correct_answer': u'tcp dump'}, {'correct_answer': u'disabled'}]
how can combine 3 list result similar this?
[{'question': u'what tool f5 bigip packet traces. ', 'answer': u'qkview', 'correct_answer': u'tcp dump'}]
another option if above problem not achievable
list1 = ['fan offline information can found on screen under general menu? ', 'what tool f5 bigip packet traces. ', 'on http health monitor configuration. if receive string , disabling string matched , reverse enabled. status of pool members?'] list2 = ['systeminfoscreen', 'qkview', 'offline'] list3 = ['systeminfoscreen', 'tcp dump', 'disabled']
merging 3 same result of:
[{'question': u'what tool f5 bigip packet traces. ', 'answer': u'qkview', 'correct_answer': u'tcp dump'}]
ps
i using python 2.7.10
zip
dictionary items in lists. transform dictionaries list of key-value tuples
, merge lists using +
, transform merged list dictionary:
[dict(i.items()+j.items()+k.items()) i, j, k in zip(list1, list2, list3)]
in python 3.x, need call list
on dict_items
:
[dict(list(i.items())+list(j.items())+list(k.items())) i,j,k in zip(list1, list2, list3)]
result:
[{'answer': 'systeminfoscreen', 'question': 'fan offline information can found on screen under general menu? ', 'correct_answer': 'systeminfoscreen'}, {'answer': 'qkview', 'question': 'what tool f5 bigip packet traces. ', 'correct_answer': 'tcp dump'}, {'answer': 'offline', 'question': 'on http health monitor configuration. if receive string , disabling string matched , reverse enabled. status of pool members?', 'correct_answer': 'disabled'}]
dictionary items not ordered, each dictionary may not go in question-answer-correctanswer order. order may different.
Comments
Post a Comment