0

I am expecting a logic table like output and I do get this output if I convert the settings dict to str first:

with settings = str(settings)

I get

[  
"{'l1': 0, 'lp1': **0**}",   
"{'l1': 0, 'lp1': **1**}",   
"{'l1': 1, 'lp1': **0**}",   
"{'l1': 1, 'lp1': **1**}"]

This is a simplified code block that exhibits the same behaviour

l_params = {
    "l1": [0,1]
}

lp_params = {
    "lp1": [0,1]
}

def grid_parameters(parameters: dict[str, Iterable[Any]]) -> Iterable[dict[str, Any]]:
    for params in product(*parameters.values()):
        yield dict(zip(parameters.keys(), params))

outlist = []

for l_settings in grid_parameters(l_params):
    for lp_settings in grid_parameters(lp_params):
        settings = l_settings
        settings['lp1'] = lp_settings['lp1']
        outlist.append(settings)
        print('ls', outlist)

with this I get:

[  
{'l1': 0, 'lp1': **1**},   
{'l1': 0, 'lp1': **1**},  
{'l1': 1, 'lp1': **1**},  
{'l1': 1, 'lp1': **1**}]

If I print out outlist in each iteration we see that the correct data is appended, but in the next iteration that data has been change?

ls [{'l1': 0, 'lp1': **0**}]  
ls [{'l1': 0, 'lp1': **1**}, {'l1': 0, 'lp1': 1}]  
ls [{'l1': 0, 'lp1': 1}, {'l1': 0, 'lp1': 1}, {'l1': 1, 'lp1': **0**}]  
ls [{'l1': 0, 'lp1': 1}, {'l1': 0, 'lp1': 1}, {'l1': 1, 'lp1': **1**}, {'l1': 1, 'lp1': 1}]  

I have used the cast to str, to get around this but it would be really cool to understand why this is happening?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `settings = l_settings` doesn't make a copy of the dictionary. Both variables are references to the same dictionary, and you keep modifying it in place each time through the loop. Use `settings = l_settings.copy()` – Barmar May 08 '23 at 15:12
  • `settings = l_settings` doesn't create a copy, it makes both variables reference ths same object. So, each inner loop, you keep altering the original AND all the other references to it (which you mistakenly *expect* to be discrete copies). Look into using `.copy()` – MatBailie May 08 '23 at 15:13

0 Answers0