Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested dicts inside nested dicts, how to automate them?

I am trying to generate a nested dict, inside another nested dict, but I could only get one step done.

Here is the code i got:

variables = ['CHG', 'Open', 'Close']
assets= ['GOOG', 'FB']
dates =['28-09-2020', '25-09-2020']

dct = {x: dict(zip(assets, '0' * len(assets))) for x in dates}

Gets me:

{'28-09-2020': {'GOOG': '0', 'FB': '0'}, '25-09-2020': {'GOOG': '0', 'FB': '0'}}

How can i generate the dict, with variables inside each asset?

My objective is to get a dict like this:

{'28-09-2020': {'GOOG': {'CHG':0, 'Open':0, 'Close':0}, 'FB': {'CHG':0, 'Open':0, 'Close':0}}, '25-09-2020': {'GOOG': {'CHG':0, 'Open':0, 'Close':0}, 'FB': {'CHG':0, 'Open':0, 'Close':0}}}
like image 701
guialmachado Avatar asked Oct 28 '25 08:10

guialmachado


1 Answers

{date: {x: dict(zip(variables, '0' * len(variables))) for x in assets} for date in dates}

Output

{'28-09-2020': {'GOOG': {'CHG': '0', 'Open': '0', 'Close': '0'},
  'FB': {'CHG': '0', 'Open': '0', 'Close': '0'}},
 '25-09-2020': {'GOOG': {'CHG': '0', 'Open': '0', 'Close': '0'},
  'FB': {'CHG': '0', 'Open': '0', 'Close': '0'}}}
like image 54
Chris Avatar answered Oct 29 '25 21:10

Chris