UPDATE
Thanks for UltraInstinct and Mankind_008 reminder, a more simplified and intrinsic answer is addressed below:
lst = [{"qty": 12}, {"qty": 12}, {"qty": 12}, {"qty": 12}, {"qty": 12}, {"qty": 12}, {"fail": 0, "pass": 12},
{"fail": 0, "pass": 12}, {"fail": 1}, {"pass": 11}, {"fail": 1}, {"pass": 11}, {"fail": 1}, {"pass": 11},
{"fail": 2}, {"pass": 10}]
# separate dictionaries with different keys
# dictionaries containing both "fail" and "pass" keys will be split
# and fitted into "fail_group" and "pass_group" respectively
qty_group = ({key: _} for dic in lst for key, _ in dic.items() if key == "qty")
fail_group = ({key: _} for dic in lst for key, _ in dic.items() if key == "fail")
pass_group = ({key: _} for dic in lst for key, _ in dic.items() if key == "pass")
# merge dictionaries with keys "qty", "fail" and "pass" item-wisely.
# and print result
print(list({**x, **y, **z} for (x, y, z) in zip(qty_group, fail_group, pass_group)))
Note that {**x, **y, **z}
only work on python >= 3.5, which was introduced in PEP 448. For python 2 or python <3.5, you have to define your custom function to do the same thing as {**x, **y, **z}
(more details are discussed in this thread):
def merge_three_dicts(x, y, z):
m = x.copy()
n = y.copy()
z.update(x)
z.update(y)
return z
So in this scenario, the last line of the code should be:
print(list(merge_three_dicts(x, y, z) for (x, y, z) in zip(qty_group, fail_group, pass_group)))
Both the methods mentioned above will give you the result:
[{'qty': 12, 'fail': 0, 'pass': 12}, {'qty': 12, 'fail': 0, 'pass': 12}, {'qty': 12, 'fail': 1, 'pass': 11}, {'qty': 12, 'fail': 1, 'pass': 11}, {'qty': 12, 'fail': 1, 'pass': 11}, {'qty': 12, 'fail': 2, 'pass': 10}]