0

This is the desired dictionary that I want to create

trade_stocks_dic = {datetime.date(2021,1,1) : [{'ACC': {'High':800,'Low':200}},{'TCS': {'High':1000,'Low':250}},{'RELIANCE': {'High':134,'Low':20}}] }

Here is my code

for stock in all_stocks:

data = {}
data[datetime.date(2012,1,2)] = []
data[datetime.date(2012,1,2)].append(data[stock]={'High':10,'Low':20})

It is giving me SyntaxError: keyword can't be an expression

1 Answers1

2

The exception has obviously told you that your keyword, which means your value, cannot be an expression. That is, you cannot give dict keyword such that:

data['a'].append(True = False)

Also, you have a bug in your initialization. If you do so, you will clear your data dictionary every iteration. The correction of the code should be above:

data = {}
all_stocks = ["ACC","TCS","RELIANCE"]
for stock in all_stocks:
    if datetime.date(2012,1,2) not in data:
        data[datetime.date(2012,1,2)] = [{stock: {'High':10,'Low':20}}]
    else:
        data[datetime.date(2012,1,2)].append({stock: {'High':10,'Low':20}})
konata39
  • 196
  • 6