-1

I have two dictionaries as d1 and d2. I want to create a new dictionary that I used d1 and d2 as keys on it.

d1 = {"open": 10, "close":15}
d2 = {"open":11, "close":14}

output:

D = {"d1":{"open": 10, "close":15}, "d2":{"open":11, "close":14}}
momo
  • 1

2 Answers2

0

you can create D like so:

D = {"d1":d1, "d2":d2}

although if you change the values in either d1 or d2 after you define D, it will also change the corresponding values in D because D will have shared references to d1 and d2. if you don't want this you can use copies

D = {"d1":d1.copy(), "d2":d2.copy()}
Hadrian
  • 917
  • 5
  • 10
0

You could do this:

d1 = {"open":10, "close":15}
d2 = {"open":11, "close":14}

def tostr(**kwargs):
    return kwargs

D = tostr(d1=d1, d2=d2)
print(D)

The output is

{'d1': {'open': 10, 'close': 15}, 'd2': {'open': 11, 'close': 14}}

yoonghm
  • 4,198
  • 1
  • 32
  • 48