Aman Gupta
Aman Gupta

Reputation: 1874

How to merge multiple dictionaries

I have main_dict.

main_dict={'name1':{'key1':'value1', 'key2':'value2'}, 'name2':{'key1':'value3', 'key2':'value8'} ... }

I have 2 other dictionaries which brings some more data to be added in the main_dict.

like,

**age_dict= {{'age':'age_value1', 'name': 'name1'}, {'age':'age_value1', 'name': 'name2'}}
gender_dict= {{'gender':'gen_value1', 'name': 'name1'}, {'gender':'gen_value2', 'name': 'name2'}}**

Now i would like to make some loops and merge these dictionaries such that it checks for the same name and takes values from age and gender dictionaries and create keys 'age' , 'gender' and add them into main_dict.

For now i have done this, but i think django can help to do this in a single way:

for user in age_dict:
    for key, value in main_dict.iteritems():
        if key == user['name']:
            value['age'] = user['age_value']

for user in gender_dict:
    for key, value in main_dict.iteritems():
        if key == user['name']:
            value['gender'] = user['gen_value']

EDIT: Modified age_dict and gender_dict.

Upvotes: 0

Views: 2138

Answers (3)

SuReSh
SuReSh

Reputation: 1511

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():
        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():
        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():
        super_dict[k].add(v)

Upvotes: 1

Jasper
Jasper

Reputation: 3947

General hint: if you are doing something like

for key, val in some_dict.iteritems():
    if key == some_value:
       do_something(val)

you are most likely doing it wrong, because you are not using the dictionaries very purpose: accessing elements by their keys. Instead, do

do_something(some_dict[key])

and use exceptions if you can't be sure that somedict[key] exists.


You don't have to interate over dictionaries to find the appropriate key. Just access it directly, that's what dictionaries are for:

main_dict={'name1':{'key1':'value1', 'key2':'value2'}, 'name2':{'key1':'value3', 'key2':'value8'}}


age_dicts = [{'age':'age_value1', 'name': 'name1'}, 'age':'age_value1', 'name': 'name2'}]
gender_dicts = [{'gender':'gen_value1', 'name': 'name1'}, 'gender':'gen_value2', 'name': 'name2'}]

for dct in age_dicts:
    main_dict[dct['name']]['age'] = dct['age']

for dct in gender_dicts:
    main_dict[dct['name']]['gender'] = dct['gender']

Specific answer to the pre-edit case:

age_dict= {'name1':'age_value1', 'name2':'age_value2'}
gender_dict= {'name1':'gen_value1', 'name2':'gen_value2'}

If you are sure that gender_dict and age_dict provide values for each name, it's as easy as

for name, dct in main_dict.iteritems():
   dct['age'] = age_dict[name]
   dct['gender'] = gender_dict[name]

If there are names without entries in the other dictionaries, you can use exceptions:

for name, dct in main_dict.iteritems():
   try:
       dct['age'] = age_dict[name]
   except KeyError:    # no such name in age_dict
       pass
   try:
       dct['gender'] = gender_dict[name]
   except KeyError:    # no such name in gender_dict
       pass

Upvotes: 1

Salva
Salva

Reputation: 6837

So you want use age_dict and gender_dict to enrich the values for the keys in main_dict. Well, given Python guarantees average dict lookup to be constant you are constrained only by the number of keys in main_dict and you can reach the enrichment in O(n) where n is the size of the dictionary:

for user_name, user_info in main_dict.items():
  if user_name in gender_dict:
    user_info['gender'] = gender_dict[user_name]
  if user_name in age_dic:
    user_info['age'] = age_dict[user_name]

And a fancy function doing this in a generic way:

def enrich(target, **complements):
  for user_name, user_info in target.items():
    for complement_key, complemented_users in complements.items():
      if user_name in complemented_users:
        user_info[complement_key] = complemented_users[user_name]

enrich(main_dict, age=age_dict, gender=gender_dict)

Even if you see two nested loops, it is more likely the number of users in main_dict dominates over the number of complementary dictionaries.

Upvotes: 0

Related Questions