# For each item in list - variables not storing

**URL:** https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097
**Category:** Questions about Thunkable X
**Created:** [June 8, 2023, 9:28pm UTC](https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097 "2023-06-08T21:28:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![trinilopez994](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@trinilopez994](https://community.thunkable.com/u/trinilopez994)
#### Post date: [June 8, 2023, 9:28pm UTC](https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097/1 "2023-06-08T21:28:07Z")

</div>

Hello,

I’m trying to set a series of variables using a “For each item j in list” block. While the loop is running, I can see the integers being generated. However, at the end of the loop, all the variables are set to 0. What am I doing wrong? How can I get the variables to retain their assigned values after the loop completes?

 ![Screenshot 2023-06-08 at 2.26.32 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/e/4ee45c2ae79bc653274b44e902a02b668c6f9edf.png)  
 ![Screenshot 2023-06-08 at 2.25.22 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/b/9b852d86251a83f6845795e6b697dad450e69cff.png)

---

<div class="post-metadata">

### Author: ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)
#### Post date: [June 8, 2023, 11:08pm UTC](https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097/2 "2023-06-08T23:08:08Z")

</div>

Your blocks set a single variable to a random number each iteration of the loop. So j is only going to have the last value that was set when the loop ends.

Instead of storing every value in the same loop variable (variables like j are really only to be used inside of a loop block), store the random integer as a list item. Use the `insert at last` block ([Lists Blocks - Thunkable Docs](https://docs.thunkable.com/lists#insert-at-set)) from the List drawer to add an item to the end of the list during each iteration of the loop. You actually don’t need all of those separate variables. You can just store the list in a single variable that is a list (initialize it as an empty list or set it to an empty list when the button is clicked. Create a variable called something like valuesList and then use that with the `insert at last` block.

If you want to generate 6 random numbers and store them, you don’t need to use the `for each item` block. You can just use the `count with i from 1 to 6` block.

---

<div class="post-metadata">

### Author: ![trinilopez994](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@trinilopez994](https://community.thunkable.com/u/trinilopez994)
#### Post date: [June 9, 2023, 5:26pm UTC](https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097/3 "2023-06-09T17:26:09Z")

</div>

Awesome thank you!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)
#### Post date: [September 7, 2023, 5:26pm UTC](https://community.thunkable.com/t/for-each-item-in-list-variables-not-storing/2458097/4 "2023-09-07T17:26:38Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
