-1

I have a list of python dictionaries, looking like this:

[{key1: value1, key2: array1}, {key1: value2, key2: array2}, {key1: value3, key3: array3},...]

In my case, some of these dictionaries have the same value for key1, for example, value1 = value3. How can I end up getting an array of dictionaries that will look like this?

 [{key1: value1, key2: array1+array3}, {key1: value2, key2: array2},...]

where "array1+array3" is a single array with appended elements of the original array1 and array3 arrays?

edit: this is not merging by key; each dictionary in the array has strictly the same structure: {key1: "some_string", key2: [an array]} - my problem is that I wish to "merge" them on the "some_string", so the [an array], the value of key2, will be concatenated with arrays from other dictionaries in the list that have the same "some_string" as value of key1.

nvergos
  • 432
  • 3
  • 15
  • Possible duplicate of [How can I merge two Python dictionaries in a single expression?](http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression) – Bohdan Ivanov Jul 28 '16 at 18:37

2 Answers2

0

The best way to do what you want is to re-structure your data, at least temporarily. If you were to use a dictionary that mapped from value to array (without the fixed keys), you could put all the data into a single place and efficiently detect when duplicate values occur so that you can merge the values together. If need be, you can turn the single dictionary back into a list of smaller dictionaries afterwards.

Try something like this:

merged = {}
for d in list_of_dicts:
    if d["key1"] not in merged:
        merged[d["key1"]] = d["key2"]
    else:
        merged[d["key1"]] += d["key2"]

# if necessary, put back into a list of dicts:
merged_list = [{"key1": k, "key2": v} for k, v in merged.items()]

The order of the list at the end will be arbitrary. If you want to preserve the order of the dicts in the original list, you could use an OrderedDict for merged.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

Since you want to group by key1, you can use itertools.groupby().

from operator import itemgetter
from itertools import groupby

list_of_dicts = [{'key1': 1, 'key2': [1, 2]},
                 {'key1': 2, 'key2': [3, 4]},
                 {'key1': 1, 'key2': [5, 6]}]

grouped = groupby(sorted(list_of_dicts, key=itemgetter('key1')),
                  key=itemgetter('key1'))
result = [{'key1': k,
           'key2': [elt for item in g for elt in item['key2']]}
          for k, g in grouped]
print(result)

Output:

[{'key2': [1, 2, 5, 6], 'key1': 1}, {'key2': [3, 4], 'key1': 2}]
Alicia Garcia-Raboso
  • 13,193
  • 1
  • 43
  • 48