2

In the code below I create a dictionary 3 times. I want to change the value of Strategy 2's Ontrade value. I am trying to to get the Expected Output below where Strategy 2's Ontrade value is equivalent to False . How would I be able to change that?

import numpy as np 

listrange = 3
data = {}
dict_integrity = {"Array": np.array([]), "last_timestamp": 0, "last_Trades": 0, "Order_id_list": 0, "Ontrade": True, "TakeProfits": 0, "StopLoss": 0}

for x in range(listrange):
    data["Strategy " + str(x)] = dict_integrity

alteration = data["Strategy " + str(2)]["Ontrade"] = False

Output:

{'Strategy 0': {'Array': array([], dtype=float64),
                'Ontrade': True,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0},
 'Strategy 1': {'Array': array([], dtype=float64),
                'Ontrade': True,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0},
 'Strategy 2': {'Array': array([], dtype=float64),
                'Ontrade': True,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0}}

Expected Output:

{'Strategy 0': {'Array': array([], dtype=float64),
                'Ontrade': True,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0},
 'Strategy 1': {'Array': array([], dtype=float64),
                'Ontrade': True,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0},
 'Strategy 2': {'Array': array([], dtype=float64),
                'Ontrade': False,
                'Order_id_list': 0,
                'StopLoss': 0,
                'TakeProfits': 0,
                'last_Trades': 0,
                'last_timestamp': 0}}
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
georgehere
  • 610
  • 3
  • 12
  • Because **you keep adding the same dictionary as the value to every key**: `data["Strategy " + str(x)] = dict_integrity`. You must create a *new dictionary*, and likely, you want all the values to be new objects as well – juanpa.arrivillaga Jul 26 '21 at 15:24
  • In other words, "In the code below I create a dictionary 3 times" is wrong. You create that dict exactly once – juanpa.arrivillaga Jul 26 '21 at 15:25

2 Answers2

1

The reason why it doesn't work is because you are assigning the new dictionary with a reference to the old dictionary. So when you change the value in one part of the new dictionary you are really just changing the original dictionary in which python uses to get info for all parts of the new dictionary.

To fix this you need to make a copy of the original dictionary which can be done like this.

data["Strategy " + str(x)] = dict(dict_integrity)

If you have any further questions, this link will help. How to copy a dictionary and only edit the copy

As a side note you should just have

data["Strategy " + str(2)]["Ontrade"] = False

and drop the "alteration =" part when changing your values

ChristopherOjo
  • 237
  • 1
  • 12
-2

You can do this by simply adding this line - data["Strategy " + str(2)]["Ontrade"] = False.
this will work and now data have update value.

Nitesh yadav
  • 11
  • 1
  • 4