# How to convert key of array from int to string through scripting

**URL:** <https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392>\
**Category:** Ignition\
**Created:** [December 22, 2021, 1:29pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392 "2021-12-22T13:29:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![manjuyadav95.m](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@manjuyadav95.m](https://forum.inductiveautomation.com/u/manjuyadav95.m)\
**Post date:** [December 22, 2021, 1:29pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/1 "2021-12-22T13:29:49Z")

</div>

I want the row key to be displayed as below:  
[“Name”, “percentage”, “color”]

below is my code:

```auto
rows = [round(value1,2),round(value2,2),round(value3,2),round(value4,2)]
		
		vList = ['Value1','Value2','Value3','Value4']
		headers=['Columns','Values', 'Color']
		data= []
		for x in range(len(vList)):
			if rows[x]<75:
				row = [vList[x], rows[x], "red"]#to change individual bar chart color based on value
			else:
				row = [vList[x], rows[x], "green"]#to change individual bar chart color based on value
			data.append(row)
		
		self.parent.parent.getChild("cntr").getChild("Chart").props.dataSources.example = data

```

output is:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/a/1ad6354e2744660c0edf942bacb066f207eb5f92.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/8/e8bde3346edf432d84130323606094f4d8d45dfb.png)

Expectd output is:  
Name:Value1  
percentage:3.78  
color:red

but I am getting output as  
0:Value1  
1:3.78  
2:red

@victordcq plz help me on this how to change the key of an array through scripting

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 22, 2021, 1:37pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/2 "2021-12-22T13:37:28Z")

</div>

> [@manjuyadav95.m](#):
>
> change the key of an array

You cant, but you want to use an array of dicts here. Not an array of arrays  
`row = {"Name":vList[x], "percentage":rows[x], "color":"red"}`

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 22, 2021, 2:41pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/3 "2021-12-22T14:41:30Z")

</div>

> [@manjuyadav95.m](#):
>
> ```auto
> for x in range(len(vList)):
> vList[x]
> 
> ```

Don't do that in python.

```python
for value in value_list:
   value

```

Even if you need the index, `enumerate()` is what you want:

```python
for index, value in enumerate(value_list):

```

and if you wanted the index to iterate through something else in parallel, python has got you covered again:

```python
for v1, v2 in zip(list1, list2):

```

Now, about your script. As Victor said, you're probably better of using a list of dicts.  
I'd suggest this for your script, to try to stay close to your initial draft:

```python
values = [round(value1,2),round(value2,2),round(value3,2),round(value4,2)]
names = ['Value1','Value2','Value3','Value4']
data = [
	{
		'name': name,
		'value': value,
		'color': 'red' if value < 75 else 'green'
	} for name, value in zip(names, values)
]

```

---

<div class="post-metadata">

**Author:** ![manjuyadav95.m](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@manjuyadav95.m](https://forum.inductiveautomation.com/u/manjuyadav95.m)\
**Post date:** [December 28, 2021, 5:29am UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/4 "2021-12-28T05:29:18Z")

</div>

thanks alot for your replies @victordcq @pascal.fragnoud

This is my earlier code... I was not able to change the color of the individual bar chart that's y wanted to convert into an object/array and wanted to change the key of the same as the x-axis  
and y-axis prop were not accepting name as integer 0/1 and by using transform I converted the datatype which is a complication and every time I need to open the designer and open the prop and save the conversion to make it work

> [@manjuyadav95.m](#):
>
> ```auto
> rows = [round(value1,2),round(value2,2),round(value3,2),round(value4,2)]
> 		
> vList = ['Value1','Value2','Value3','Value4']
> headers=['Columns','Values', 'Color']
> data= []
> for x in range(len(vList)):
> if rows[x]<75:
> row = [vList[x], rows[x], "red"]#to change individual bar chart color based on value
> else:
> row = [vList[x], rows[x], "green"]#to change individual bar chart color based on value
> data.append(row)
> 		
> self.parent.parent.getChild("cntr").getChild("Chart").props.dataSources.example = data
> 
> ```

This is the final code which is working as expected ...in this, I converted it into a dataset that didn't work earlier due to a small mistake from my end ..now correction made and working fine 😅

```auto
rows = [round(value1,2),round(value2,2),round(value3,2),round(value4,2)]
		
		vList = ['Value1','Value2','Value3','Value4']
		headers=['Columns','Values', 'Color']
		data= []
		for x in range(len(vList)):
			if rows[x]<75:
				row = [vList[x], rows[x], "red"]#to change individual bar chart color based on value
			else:
				row = [vList[x], rows[x], "green"]#to change individual bar chart color based on value
			data.append(row)
		finalData=system.dataset.toDataSet(headers,data)
		self.parent.parent.getChild("cntr").getChild("Chart").props.dataSources.example = finalData

```

and Under Series prop\> column\>appearance\>below path in img\> given column name Color to get the color from column based on the values  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/6/962cbc06540b6f0d39d632f124fcc66a5472d7e0.png)

Once again thanks 👍 🙂

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 5, 2022, 9:01am UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/5 "2022-01-05T09:01:20Z")

</div>

> [@manjuyadav95.m](#):
>
> ```python
> for x in range(len(vList)):
> if rows[x]<75:
> row = [vList[x], rows[x], "red"]
> else:
> row = [vList[x], rows[x], "green"]
> data.append(row)
> 
> ```

let me insist on this: Don't use `for x in range(len(list))`  
Let's do this step by step.  
If you're iterating over a list, `for item in list` will access each item directly, without having to use the index.  
If you need to iterate through more than one iterable in parallel, `zip(iter1, iter2)` is what you need.

```python
a = [1, 2, 3, 4]
b = ['a', 'b', 'c', 'd']
print(zip(a, b))
# outputs [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

```

So, zip will produce a list of tuples that contain the items at a specific index from every iterable.  
those are equivalent:

```python
for item_a, item_b in zip(a, b):
    do_something_with_a(item_a)
    do_something_with_b(item_b)

for i in range(len(a)):
    do_something_with_a(a[i])
    do_something_with_b(b[i])

```

So, in your case, you can replace your for loop with this:

```python
for vList_item, rows_item in zip(vList, rows):
	if rows_item < 75:
		row = [vList_item, rows_item, "red"]
	else:
		row = [vList_item, rows_item, "green"]
	data.append(row)

```

It's the pythonic way of looping over several iterables.  
Now, I suggest a few other modifications that make code more readable (at least for me):  
With basic `if/else` structures, you can actually make it just one line.

```python
if a:
    x = 1
else:
    x = 2

```

can be written like this:

```python
x = 1 if a else 2

```

So your loop can now be reduced to

```python
for vList_item, rows_item in zip(vList, rows):
	color = "red "if rows_item < 75 else "green"
	row = [vList_item, rows_item, color]
	data.append(row)

```

which can be further simplified using comprehensions, which may or may not make things easier to read, depending on a lot of factors.  
And finally, one last thing you should consider: Find better names for your variables, especially when dealing with iterables that you're gonna loop over.

---

<div class="post-metadata">

**Author:** ![manjuyadav95.m](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@manjuyadav95.m](https://forum.inductiveautomation.com/u/manjuyadav95.m)\
**Post date:** [January 6, 2022, 9:25am UTC](https://forum.inductiveautomation.com/t/how-to-convert-key-of-array-from-int-to-string-through-scripting/54392/6 "2022-01-06T09:25:51Z")

</div>

Thank u so much 🙂 😍 👍
