# \`set\_global\` for nested loops doesn't work how I expect

**URL:** <https://zola.discourse.group/t/set-global-for-nested-loops-doesnt-work-how-i-expect/1677>\
**Category:** Support\
**Created:** [March 11, 2023, 10:42pm UTC](https://zola.discourse.group/t/set-global-for-nested-loops-doesnt-work-how-i-expect/1677 "2023-03-11T22:42:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![spence](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/spence/32/1022_2.png) [@spence](https://zola.discourse.group/u/spence)\
**Post date:** [March 11, 2023, 10:42pm UTC](https://zola.discourse.group/t/set-global-for-nested-loops-doesnt-work-how-i-expect/1677/1 "2023-03-11T22:42:39Z")

</div>

Sorry, this seems to be more of a tera question. I noticed that `set_global` doesn’t seem to work as I would expect, when used in the context of nested for loops.

For example, consider a simple `i`, `j` nested for loop, where we’re creating a list for every `i` iteration, and then using `set_global` to concat to the list within the `j` iteration. Like this:

```plaintext
{% for i in [1, 2, 3] %}
  {% set list = [] %}
  {% for j in [4, 5, 6] %}
    {% set_global list = list | concat(with=j) %}  
  {% endfor %}    
  {{list}}
{% endfor %}

```

The output of `{{list}}` here is:

```plaintext
[] [] []

```

This isn’t really what I expect would happen at all. Is this a bug in tera or am I misunderstanding something?

If I move the creation of the list outside of both loops, it behaves as expected. For example:

```plaintext
{% set list = [] %}  
{% for i in [1, 2, 3] %}  
  {% for j in [4, 5, 6] %}
    {% set_global list = list | concat(with=j) %}  
  {% endfor %}    
  {{list}}
{% endfor %}

```

Results in:

```plaintext
[4, 5, 6] [4, 5, 6, 4, 5, 6] [4, 5, 6, 4, 5, 6, 4, 5, 6]

```

Thank you

P.S: There is a workaround, which is to declare the list outside of both loops, and then use `set_global` in the outer loop to make the list empty, and then update it within the inner loop. This achieves the same effect, but I was still surprised to see this and think that I have to be misunderstanding something. E.g.

```plaintext
{% set list = [] %}  
{% for i in [1, 2, 3] %}
  {% set_global list = [] %}
  {% for j in [4, 5, 6] %}
    {% set_global list = list | concat(with=j) %}  
  {% endfor %}      
  {{list}}
{% endfor %}

```

Makes:

```plaintext
[4, 5, 6] [4, 5, 6] [4, 5, 6]

```

Which is what I expect
