I have a dict variable that I use to store my default values.
I also have a dict variable that I use for custom values that change depending on a variety of circumstances.
I want to combine the two objects into a new dict using theupdatefunction.
I do this so that the new dict uses all of my custom values, and if any custom values are not present there, the values from the default object are used.
For example, thedefault_dictvariable has"x": "1", "y": "2"
And the custom_dict variable has"x": "8"
The new combined dict that I make withsetcombined_dict= default_dictand thendo combined_dict.update(custom_dict)would have the value:"x" "8", "y": "2"
The problem is that after I successfully do this, my originaldefaul_dictvariable has it’s properties updated with the values from thecustom_dictvariable. Sodefault_dict and combined_dictend up being identical.
How do I stop that from happening? Here’s my reduced test code:
{% set default_dict = { "padding": 40 } %}
{% set custom_dict = { "padding": 100 } %}
<b>default_dict:</b> {{ default_dict }}<br> {# resolves to 40 #}
<b>custom_dict:</b> {{ custom_dict }}<br> {# resolves to 100 #}
{% set combined_dict = default %}
{% do combined_dict.update(custom_dict) %}
<b>combined_dict:</b> {{ combined_dict }}<br> {# resolves to 100 #} <b>default_dict:</b> {{ default_dict }}<br> {# also resolves to 100, I want 40 #}
<b>custom_dict:</b> {{ custom_dict }}<br> {# resolves to 100 #}
Hey, @jkupczak👋 I think the issue is that you're not creating a new dictionary; you're creating a reference to the same dictionary in the object memory. Have you tried creating a copy of `default_dict` before updating it with `custom_dict`? To make sure that `combined_dict` is a separate dictionary, and changes won't affect `default_dict`.
Hey, @jkupczak👋 I think the issue is that you're not creating a new dictionary; you're creating a reference to the same dictionary in the object memory. Have you tried creating a copy of `default_dict` before updating it with `custom_dict`? To make sure that `combined_dict` is a separate dictionary, and changes won't affect `default_dict`.