fx0123 fx0123 - 2 months ago 4
Python Question

how to merge two dict in python?

I have a dict struct such like this:

a = {'1' : {}, '2' : {}}

b = {'3' : 3, '4' : 4}


I want to have the following format:

a = {'1' : { '3' : 3 }, '2' : { '4' : 4 } }


I have tried many times, but I always get lost, could anybody help me?

Answer

This works but what's the use case? Seems like there probably is a more elegant solution.

a = {i:{j:k} for i,(j,k) in zip(a.keys(),b.items())}
Comments