0

I have a small problem with creating my code. I am trying to merge two dictionaries in Python. Each of them has set of keys lets say:

a={person_id:xx, address_id: xx, name:xx}
b={address_id:xx, street:xx, postcode:xx, town:xx}

Each of them has many entries and by that I mean they look like:

a={person_id:1. address_id:20, name:john; person_id:2, address_id:200, 
   name: mary';... and 10000 more entries like that}

b={address_id:20... same situation like in a}

I would like to get:

merged_dic={person_id:xx, address_id {street:xx, postcode:xx, town:xx}, name:xx; person_id:xxx.. and so on}

I tried so many different things, update, defaultdict and many more and none of them worked. I don't want to overwrite things, I just want to create some sort of nested dict.

Any ideas on how to proceed?

5
  • 1
    some sort of nested dictionary? That is rather vague. Commented Feb 12, 2018 at 5:48
  • sorry for that, what I meant is that I wanted to get all the 10k values listed in the form of merged_dic={person_id:xx, address_id {street:xx, postcode:xx, town:xx}, name:xx; person_id:xxx.. and so on}, I don't really know how to call it properly, still a beginner and trying to learn. Tbh whatever I am trying to do it does not show all the values properly, either overwrites it or shows just a limited number like the last value of dictionary a and not all of them. Commented Feb 12, 2018 at 5:50
  • Could you please post actual Python data structure literals (e.g. what does the semicolon in the dict?). Your a dict has duplicate keys and one can't c'n'p this code. Commented Feb 12, 2018 at 5:53
  • I dont understand your dictionary, you should explain it more. Commented Feb 12, 2018 at 6:14
  • Hi, yes there are multiple, duplicate keys (around a few thousand entries) but with different values in both dictionaries. Commented Feb 13, 2018 at 5:30

1 Answer 1

3

To merge the dict you might want to try the dict.update() method like:

Code:

def merge_my_dicts(a_dict, b_dict):
    # verify the addresses match
    assert b_dict['address_id'] == a_dict['address_id']

    # get a copy of the address dict and remove extra
    address = b_dict.copy()
    del address['address_id']

    # get a copy of the main dict, and update with the address
    new_dict = a_dict.copy()
    new_dict.update({'address_id': address})
    return new_dict

Test Code:

a = {'person_id': 'pid', 'address_id': 'aid', 'name': 'nm'}
b = {'address_id': 'aid', 'street': 'st', 'postcode': 'pc', 'town': 'tn'}
print(merge_my_dicts(a, b))

Results:

{
    'person_id': 'pid', 
    'address_id': {
        'street': 'st', 
        'postcode': 'pc', 
        'town': 'tn'},
    'name': 'nm'
}
Sign up to request clarification or add additional context in comments.

2 Comments

It is a good option, thanks! But unfortunately, it only works if dicts do not have duplicate keys with different values and these ones have. I tried the update and it only shows the last entry and I want to get all of them.
@Vesh, you are very welcome. However, on SO the very best way to say thanks is to upvote any questions or answers you find useful. And on your questions, if one of the answers is a good fit for your problem, you can mark it as the accepted answer. See the Help Center for guidelines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.