Home > Mobile >  Merge dicts from list with getting set of values for equal keys
Merge dicts from list with getting set of values for equal keys

Time:05-19

Do I have any opportunity to rewrite the code below with dict enhancement? (if I name it right, mean {k: v for k, v in ...})

list_of_dicts = [{'a': 1}, {'b': 2}, {'b': 20, 'c': 3}, {'a': 10, 'b': 2}]
for k, v in [p for d in list_of_dicts  for p in d.items()]:
    d[k] = d.setdefault(k, set()) | {v}

CodePudding user response:

sure why not :). but it is nested at bit

  • Related