-4

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?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
fx0123
  • 13
  • 3

2 Answers2

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

>>> c
{'2': {'3': 3}, '1': {'4': 4}}

There is no order in a dictionnary so you can't ensure that {3:3} or {4:4} is the value of key '1' or "2'

Jal
  • 205
  • 1
  • 2
  • 8
0

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())}
Joe Flip
  • 1,076
  • 4
  • 21
  • 37
  • [Read the documentation on built-ins like `dict()`.](https://docs.python.org/2/library/functions.html) And click the checkmark to accept an answer! – Joe Flip Sep 22 '16 at 15:26
  • It does not workd, see@Jal – fx0123 Sep 22 '16 at 15:33
  • It solves the problem you presented. It's just not a good way of doing these things generally. That's why I asked for the use case! – Joe Flip Sep 22 '16 at 15:36
  • Sorry , it solves the prolem i presented, the use case is that I want to sort 'a in terms of some 'key' in 'a', and get some useful information. Thank you very much. – fx0123 Sep 22 '16 at 15:41