Abhishek Bhatia
Abhishek Bhatia

Reputation: 577

Merging a list of dicts into one dict with items appended to a list

def merge_dicts(list_of_dicts: list, missval=None):
    '''Merges a list of dicts, having common keys into a single dict
    with items appended to a list

    >>> d1 = {'a' : 1, 'b': 2, 'c': 3}
    >>> d2 = {'a':4, 'b':5 }
    >>> d3 = {'d': 5}
    >>> merge_dicts([d1, d2, d3], 'NA')
    {'a': [1, 4, 'NA'], 'b': [2, 5, 'NA'],
    'c': [3, 'NA', 'NA'], 'd': ['NA', 'NA', 5]}
    '''
    all_keys = []
    for d in list_of_dicts:
        for k in d.keys():
            if k not in all_keys:
                all_keys.append(k)

    merged = {}
    for k in all_keys:
        for d in list_of_dicts:
            try:
                merged[k].append(d.get(k, missval))
            except KeyError:
                merged[k] = [d.get(k)]

    return(merged)

The function docstring is self explanatory. Is there a more efficient way to do this without having to write two for loops? One to find all the keys in all the dicts, and other to make a merged dict?

Upvotes: 3

Views: 111

Answers (3)

KenHBS
KenHBS

Reputation: 7164

If you are using pandas, you could use the dictionary to fill a dataframe, and transform it back to a dictionary again:

pd.DataFrame([d1, d2, d3]).to_dict()
  {'a': {0: 1.0, 1: 4.0, 2: nan},
   'b': {0: 2.0, 1: 5.0, 2: nan},
   'c': {0: 3.0, 1: nan, 2: nan},
   'd': {0: nan, 1: nan, 2: 5.0}}

Upvotes: 0

javidcf
javidcf

Reputation: 59691

Here is one solution with defaultdict:

from collections import defaultdict

def merge_dicts(list_of_dicts: list, missval=None):
    result = defaultdict(lambda: [missval] * len(list_of_dicts))
    for i, d in enumerate(list_of_dicts):
        for k, v in d.items():
            result[k][i] = v
    return dict(result)

d1 = {'a' : 1, 'b': 2, 'c': 3}
d2 = {'a':4, 'b':5 }
d3 = {'d': 5}
print(merge_dicts([d1, d2, d3], 'NA'))
# {'a': [1, 4, 'NA'], 'b': [2, 5, 'NA'], 'c': [3, 'NA', 'NA'], 'd': ['NA', 'NA', 5]}

Upvotes: 2

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

You should use a set for creating the list of keys, if you don't care about their order. You could create it using a comprehension.

For the second part, you can use a dict comprehension, and create each list with a list comprehension:

def merge_dicts(list_of_dicts: list, missval=None):
    '''Merges a list of dicts, having common keys into a single dict
    with items appended to a list

    >>> d1 = {'a' : 1, 'b': 2, 'c': 3}
    >>> d2 = {'a':4, 'b':5 }
    >>> d3 = {'d': 5}
    >>> merge_dicts([d1, d2, d3], 'NA')
    {'a': [1, 4, 'NA'], 'b': [2, 5, 'NA'],
    'c': [3, 'NA', 'NA'], 'd': ['NA', 'NA', 5]}
    '''
    all_keys = {key for d in list_of_dicts for key in d.keys()}
    merged = {k: [d.get(k, missval) for d in list_of_dicts] for k in all_keys}

    return(merged)


d1 = {'a' : 1, 'b': 2, 'c': 3}
d2 = {'a':4, 'b':5 }
d3 = {'d': 5}
merge_dicts([d1, d2, d3], 'NA')


#{'a': [1, 4, 'NA'],
# 'b': [2, 5, 'NA'],
# 'c': [3, 'NA', 'NA'],
# 'd': ['NA', 'NA', 5]}

Upvotes: 4

Related Questions