Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merging several python dictionaries

I have to merge list of python dictionary. For eg:

dicts[0] = {'a':1, 'b':2, 'c':3} dicts[1] = {'a':1, 'd':2, 'c':'foo'} dicts[2] = {'e':57,'c':3}  super_dict = {'a':[1], 'b':[2], 'c':[3,'foo'], 'd':[2], 'e':[57]}     

I wrote the following code:

super_dict = {} for d in dicts:     for k, v in d.items():         if super_dict.get(k) is None:             super_dict[k] = []         if v not in super_dict.get(k):             super_dict[k].append(v) 

Can it be presented more elegantly / optimized?

Note I found another question on SO but its about merging exactly 2 dictionaries.

like image 259
jerrymouse Avatar asked Feb 23 '12 15:02

jerrymouse


People also ask

Can you merge dictionaries Python?

Using | in Python 3.9In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

How do I combine multiple Dicts?

We can merge dictionaries in one line by simply using the unpacking operator (**). We can also merge multiple dictionaries using this method.


2 Answers

You can iterate over the dictionaries directly -- no need to use range. The setdefault method of dict looks up a key, and returns the value if found. If not found, it returns a default, and also assigns that default to the key.

super_dict = {} for d in dicts:     for k, v in d.iteritems():  # d.items() in Python 3+         super_dict.setdefault(k, []).append(v) 

Also, you might consider using a defaultdict. This just automates setdefault by calling a function to return a default value when a key isn't found.

import collections super_dict = collections.defaultdict(list) for d in dicts:     for k, v in d.iteritems():  # d.items() in Python 3+         super_dict[k].append(v) 

Also, as Sven Marnach astutely observed, you seem to want no duplication of values in your lists. In that case, set gets you what you want:

import collections super_dict = collections.defaultdict(set) for d in dicts:     for k, v in d.iteritems():  # d.items() in Python 3+         super_dict[k].add(v) 
like image 94
senderle Avatar answered Oct 03 '22 05:10

senderle


from collections import defaultdict  dicts = [{'a':1, 'b':2, 'c':3},          {'a':1, 'd':2, 'c':'foo'},          {'e':57, 'c':3} ]  super_dict = defaultdict(set)  # uses set to avoid duplicates  for d in dicts:     for k, v in d.items():  # use d.iteritems() in python 2         super_dict[k].add(v) 
like image 43
Steven Rumbalski Avatar answered Oct 03 '22 07:10

Steven Rumbalski