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

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

below is my code:

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
image

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

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"}

3 Likes

Don’t do that in python.

for value in value_list:
   value

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

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:

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:

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)
]
4 Likes

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

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 :sweat_smile:

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

Once again thanks :+1: :slight_smile:

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.

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:

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:

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.

if a:
    x = 1
else:
    x = 2

can be written like this:

x = 1 if a else 2

So your loop can now be reduced to

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.

1 Like

Thank u so much :slightly_smiling_face: :heart_eyes: :+1: