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}}
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}}
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()}
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}}