# Grid variable updating in python script

**URL:** <https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200>\
**Category:** Matillion ETL\
**Created:** [October 12, 2020, 7:52pm UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200 "2020-10-12T19:52:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AkashBansal](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AkashBansal](https://matillioncommunity.discourse.group/u/AkashBansal)\
**Post date:** [October 12, 2020, 7:52pm UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200/1 "2020-10-12T19:52:35Z")

</div>

I have a list of values generated while pulling the data from REST API(Assume the list name to be X). I want to push this list values in grid variable(Assume Grid name -g and this grid has one column named -gc) so that I can use this grid values outside the python script for further execution.

I don't know how to proceed for the above problem.

Here is the code and example of list :-

X=['qwer','tyui','uiop','sdfgh']

for data in X:

&nbsp;g.append(data)

if g !=[]:

&nbsp;context.updateGridVariable('gc',g)

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Joe-Community-Manager](https://sea2.discourse-cdn.com/flex002/user_avatar/matillioncommunity.discourse.group/joe-community-manager/32/897_2.png) [@Joe-Community-Manager](https://matillioncommunity.discourse.group/u/Joe-Community-Manager)\
**Post date:** [October 19, 2020, 8:03am UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200/2 "2020-10-19T08:03:44Z")

</div>

Hello [@AkashBansal](https://matillioncommunity.discourse.group/u/AkashBansal)​

Sorry you have been unable to get your answer from our community users! I would recommend raising this on a support case with our amazing Solution Architects, they will be able to support you with this.

Please do keep us posted how you achieve this as I and the community would love to learn how!

Have a great day!

Joe

---

<div class="post-metadata">

**Author:** ![MichaelBoucht](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@MichaelBoucht](https://matillioncommunity.discourse.group/u/MichaelBoucht)\
**Post date:** [October 26, 2020, 9:42am UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200/3 "2020-10-26T09:42:55Z")

</div>

Hello [@AkashBansal](https://matillioncommunity.discourse.group/u/AkashBansal)​ ,

The handling of grid variables in matillion python components have a value per column and a list per row and all this is in one list.

Something like this:

X = ['qwer','tyui','uiop','sdfgh']

gv = []

for data in X:

#reset the 'list'

g = []

print data

#add the correct amount of grid variable 'columns'

g.append(data)

print g

#adding to final grid variable list

gv.append(g)

print gv

#loop is done, updating grid variable

context.updateGridVariable('gc',gv)

#checking if grid variable was updated as expected.

print context.getGridVariable('gc')

---

<div class="post-metadata">

**Author:** ![MarkRugs](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MarkRugs](https://matillioncommunity.discourse.group/u/MarkRugs)\
**Post date:** [March 23, 2021, 10:56pm UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200/4 "2021-03-23T22:56:38Z")

</div>

Results of a query to a grid variable in Python/Jython are not straightforward and the documentation or examples just don't seem to exist. I had to use this method for my project instead of query results to grid like I normally do.

&nbsp;

This is an example and it's the absolute minimum required to get a grid variable populated from a simple query using Python/Jython.

&nbsp;

I was getting a cast exception PyTuple to PyList with my query results when attempting to update the grid variable I had setup to hold the contents of the query. The following works (Jython) and I don't get any cast problems when running the script. I'm sure there's a much better way of doing this but I've only been using Python/Jython for about a year whereas I've been using C# for over 10.

&nbsp;

My grid variable (jv\_grid\_variable\_example) has four columns and potentially many rows.

&nbsp;

sql = "SELECT Col1, Col2, Col3, Col4 FROM DBName.Information\_Schema.Tables WHERE TABLE\_TYPE LIKE '%TABLE%'"

myCursor = context.cursor()

myCursor.execute(sql)

myResultSet = cursor.fetchall()

#results look like: ([u'Value1', u'Value2', u'Value3', u'Value4'], [u'Value1', u'Value2', u'Value3', u'Value4'], ...)

&nbsp;

#put the results from the query into the proper type the grid variable expects (list)

myVariablePayload = [List(i) for i in myResultSet]

#results look like: [[u'Value1', u'Value2', u'Value3', u'Value4'], [u'Value1', u'Value2', u'Value3', u'Value4'], ...]

&nbsp;

content.updateGridVariable('jv\_grid\_variable\_example', myVariablePayload)

&nbsp;

The example adds one more step to the normal process of populating variables with Python/Jython in the script task by turning the results into a list instead of a tuple. You can try on your own and use the print statements on the two variables with results to see the difference in the output.

&nbsp;

Hopefully this saves some folks from searching instead of creating.

---

<div class="post-metadata">

**Author:** ![nathan.lang1614377435484](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nathan.lang1614377435484](https://matillioncommunity.discourse.group/u/nathan.lang1614377435484)\
**Post date:** [April 20, 2021, 8:15pm UTC](https://matillioncommunity.discourse.group/t/grid-variable-updating-in-python-script/1200/5 "2021-04-20T20:15:33Z")

</div>

Thanks for this post [@MarkRugs](https://matillioncommunity.discourse.group/u/MarkRugs)​ it helped a lot.

I did find one issue and it may be due to version, but I was unable to use _[List(i) for i in myResultSet]._ I received an error I was able to accomplish the same thing by looping over the structure.
