dgilland / pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.

Home Page:http://pydash.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Can there be a feature to merge a list of dictionaries together?

benlei opened this issue · comments

Here's what I want to do:

list_of_query_results = [
  dict(id=1, foo='hello', bar='world', diff_field1='a', diff_field2='b'),
  dict(id=1, foo='hello', bar='world', diff_field1='y', diff_field2='z'),
  ...
]

pydash.merge_dict(list_of_query_results, on='id', with=['diff_field1', 'diff_field2'])

which I would like to have return:

[
  {"id": 1, "foo": "hello", "bar": "world", "diff_field1": ["a", "y"], "diff_field2": ["b", "z"]},
  ...
]

Would this be possible to do?

Is the idea to merge multiple dictionaries in a collection into a collection of unique dictionaries keyed by some primary key field (or fields) with the option to then group values from the matched dictionaries on a subset of keys? What happens with the other non-grouped fields if there are differences? Does the last one's values overwrite the previous values?