# How to update a Dictionary-Variable?

**URL:** <https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366>\
**Category:** General Discussion\
**Created:** [March 20, 2023, 4:38pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366 "2023-03-20T16:38:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![herzla](https://avatars.discourse-cdn.com/v4/letter/h/c57346/32.png) [@herzla](https://klipper.discourse.group/u/herzla)\
**Post date:** [March 20, 2023, 4:38pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/1 "2023-03-20T16:38:15Z")

</div>

hi, I only know a little python to play with klipper but until now I found what I needed

now I need a dictionary to store a list of keys and values and like to update the items by a macro

here I have my dictionary  
[gcode\_macro \_test\_variables]  
variable\_feature\_hops: {“feature1”: 1.0, “feature2”: 1.5}  
gcode:  
M115 ; must provide something

here I try to update it  
[gcode\_macro \_set\_feature\_retraction\_zhop]  
gcode:  
{% set feature\_hops = printer[“gcode\_macro \_test\_variables”].feature\_hops %}  
{% set feature = params.FEATURE|default(“”)|string %}  
{% set distance = params.DISTANCE|default(0.0)|float %}

{% if feature != “” and distance \>= 0 %}

```
#each of this lines results in an error
#{% set feature_hops |= {feature: distance} %}
#{% set feature_hops = {feature_hops, feature: distance} %}
#feature_hops.update(feature = distance)
#feature_hops[feature] = distance;    

#while this works
#{% set feature_hops = {feature: distance} %}

#saving the dict back to the macro-variable is not working also
#SET_GCODE_VARIABLE MACRO=_test_variables VARIABLE=feature_hops VALUE={feature_hops}

{action_respond_info('feature_test_zhops: {}'.format(feature_hops))}

```

{% endif %}

is it possible to read and update dictionary-items or do I have to find another solution?

---

<div class="post-metadata">

**Author:** ![koconnor](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@koconnor](https://klipper.discourse.group/u/koconnor)\
**Post date:** [March 20, 2023, 9:36pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/2 "2023-03-20T21:36:12Z")

</div>

Try something like:

```auto
SET_GCODE_VARIABLE MACRO=_test_variables VARIABLE=feature_hops VALUE='{"mykey": 123.4}'

```

-Kevin

---

<div class="post-metadata">

**Author:** ![herzla](https://avatars.discourse-cdn.com/v4/letter/h/c57346/32.png) [@herzla](https://klipper.discourse.group/u/herzla)\
**Post date:** [March 20, 2023, 10:05pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/3 "2023-03-20T22:05:21Z")

</div>

thanks, result sadly is  
Error loading template ‘gcode\_macro \_set\_feature\_retraction\_zhop:gcode’: jinja2.exceptions.TemplateSyntaxError: expected token ‘end of print statement’, got ‘:’  
same result without ‘’

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [March 21, 2023, 2:09pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/4 "2023-03-21T14:09:46Z")

</div>

`{% set dummy = feature_hops. __setitem__ (feature, distance) %}`

---

<div class="post-metadata">

**Author:** ![herzla](https://avatars.discourse-cdn.com/v4/letter/h/c57346/32.png) [@herzla](https://klipper.discourse.group/u/herzla)\
**Post date:** [March 21, 2023, 9:44pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/5 "2023-03-21T21:44:39Z")

</div>

thanks @theophile  
this really works to add an item to the dict 🙂  
now how do I write the new dictionary back to the other macro’s variable?

SET\_GCODE\_VARIABLE MACRO=\_retraction\_variables VARIABLE=feature\_hops VALUE={feature\_hops}  
returns Malformed command ‘SET\_GCODE\_VARIABLE MACRO=\_retraction\_variables VARIABLE=feature\_hops VALUE={‘featureName’: 1.0, ‘xx1’: 3.0}’  
while I think

using VALUE=‘{feature\_hops}’ returns Unable to parse ‘{featureName: 1.0, xx1: 3.0}’ as a literal

I feel so near and so far at same time 😃  
and I’ll have a deeper look into this python stuff

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [March 21, 2023, 10:00pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/6 "2023-03-21T22:00:28Z")

</div>

For that, I will refer you to this excellent post by @garethky:

> [@Macro Creation Tutorial](https://klipper.discourse.group/t/macro-creation-tutorial/30/20):
>
> something to add to the tutorial: The SET\_GCODE\_VARIABLE and SAVE\_VARIABLE commands in klipper require that you pass them Python literals. Simple strings and numbers are pretty easy but what about complex types like arrays or objects? This has been a stumbling block for more ambitious macros. If you have tried the tojson filter you have probably not had a lot of luck. Here is a way to do it using the pprint filter: [gcode\_macro TEST\_STUFF] variable\_hello: None gcode: {action\_respond\_info(p…

---

<div class="post-metadata">

**Author:** ![herzla](https://avatars.discourse-cdn.com/v4/letter/h/c57346/32.png) [@herzla](https://klipper.discourse.group/u/herzla)\
**Post date:** [March 21, 2023, 11:34pm UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/7 "2023-03-21T23:34:21Z")

</div>

awsome it works  
\_set\_feature\_retraction\_zhop feature=lala distance=5

> feature\_retraction\_zhops: {‘featureName’: 1.0, ‘lala’: 5.0}  
> \_set\_feature\_retraction\_zhop feature=lulu distance=3  
> feature\_retraction\_zhops: {‘featureName’: 1.0, ‘lala’: 5.0, ‘lulu’: 3.0}

using **getitem** to get an item is the best way also? it works fine, just the first time seems to be slow

foll code for solution:

[gcode\_macro \_retraction\_variables]  
variable\_feature\_hops: {“featureName”: 1.0}

[gcode\_macro \_set\_feature\_retraction\_zhop]  
description: set z-hop distance by feature  
gcode:  
{% set feature\_hops = printer[“gcode\_macro \_retraction\_variables”].feature\_hops %}  
{% set feature = params.FEATURE|default(“”)|string %}  
{% set distance = params.DISTANCE|default(0.0)|float %}

{% if feature != “” and distance \>= 0 %}

```
{% set dummy = feature_hops. __setitem__ (feature, distance) %}
SET_GCODE_VARIABLE MACRO=_retraction_variables VARIABLE=feature_hops VALUE="{feature_hops | pprint | replace("\n", "") | replace("\"", "\\\"")}"

{action_respond_info('feature_retraction_zhops: {}'.format(feature_hops))}

```

{% endif %}

[gcode\_macro \_get\_feature\_retraction\_zhop]  
description: set z-hop distance by feature  
gcode:  
{% set feature\_hops = printer[“gcode\_macro \_retraction\_variables”].feature\_hops %}  
{% set feature = params.FEATURE|default(“”)|string %}

{% if feature != “” %}  
{action\_respond\_info(‘feature\_retraction\_zhops: ‘+feature+’={}’.format(feature\_hops.\_\_ getitem\_\_(feature)))}  
{% endif %}

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [March 22, 2023, 12:13am UTC](https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366/8 "2023-03-22T00:13:27Z")

</div>

Yes, ` __getitem__ ` works well, and has the benefit of returning `None` instead of an error if the specified key isn’t in the dictionary for some reason. I don’t think it’s any slower than trying to access the value directly.
