CMS Development

jkupczak
Contributor

How do I preserve the values in a dict?

SOLVE
Anyone know how to solve for this?
 
  • 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 the update function.
  • 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, the default_dict variable has "x": "1", "y": "2"
  • And the custom_dict variable has "x": "8"
  • The new combined dict that I make with set combined_dict = default_dict and then do combined_dict.update(custom_dict) would have the value: "x" "8", "y": "2"
  • The problem is that after I successfully do this, my original defaul_dict variable has it’s properties updated with the values from the custom_dict variable. So default_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 #}

 

 

0 Upvotes
2 Accepted solutions
Jaycee_Lewis
Solution
Community Manager
Community Manager

How do I preserve the values in a dict?

SOLVE

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`.

 

Talk soon! — Jaycee 

 

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

View solution in original post

jkupczak
Solution
Contributor

How do I preserve the values in a dict?

SOLVE

How would I create a copy of the default dict? That's what I assumed I was doing! 😞

View solution in original post

0 Upvotes
3 Replies 3
Jaycee_Lewis
Solution
Community Manager
Community Manager

How do I preserve the values in a dict?

SOLVE

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`.

 

Talk soon! — Jaycee 

 

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

jkupczak
Contributor

How do I preserve the values in a dict?

SOLVE

Bump!

0 Upvotes
jkupczak
Solution
Contributor

How do I preserve the values in a dict?

SOLVE

How would I create a copy of the default dict? That's what I assumed I was doing! 😞

0 Upvotes