# Dataset - Looping through - How to get current rowIndex while in loop

**URL:** <https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 25, 2025, 9:00pm UTC](https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197 "2025-03-25T21:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpaisley](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rpaisley](https://forum.inductiveautomation.com/u/rpaisley)\
**Post date:** [March 25, 2025, 9:00pm UTC](https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197/1 "2025-03-25T21:00:07Z")

</div>

I want to loop through a dataset and when I find my if statement is true to get that current rowIndex and then use it to do a system.dataset.setValue().

```python
def runAction(self, event):
	newDataSet = system.dataset.toPyDataSet(self.custom.LowLevelDS)
	ITEMNMBR = self.props.selection.data[0].ITEMNMBR
	sp = event.value
	rowIndex = 0

	for row in newDataSet:		
		if row["ITEMNMBR"] == ITEMNMBR:
			newDataSet = system.dataset.setValue(newDataSet, rowIndex, 'LowLevelSP', sp)
		

	self.custom.LowLevelDS = newDataSet
	
	self.getSibling("Table_3").refreshBinding("props.data")

```

This is on a table component in perspective and when I edit the cell use an Component event and onEditCellCommit run this script to update a dataset with the new value.

Any help would be greatful.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [March 25, 2025, 9:04pm UTC](https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197/2 "2025-03-25T21:04:30Z")

</div>

Use `enumerate`:

```python
for ndx, row in enumerate(newDataSet):
	if row["ITEMNMBR"] == ITEMNMBR:
		newDataSet = system.dataset.setValue(newDataSet, ndx, 'LowLevelSP', sp)

```

> **[2. Built-in Functions — Python 2.7.18 documentation](https://docs.python.org/2/library/functions.html?highlight=enumerate#enumerate)**

I'd be very cautious about assigning the modified dataset back to the original dataset you are iterating through. I would recommend `break`ing from the loop as soon as you have found the row you need to modify.

---

<div class="post-metadata">

**Author:** ![rpaisley](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rpaisley](https://forum.inductiveautomation.com/u/rpaisley)\
**Post date:** [March 26, 2025, 11:51am UTC](https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197/3 "2025-03-26T11:51:48Z")

</div>

Thanks ryan this worked great then I added some code to change the row red if was below the LowLevelSP after changing it to a JSON.

```python
def runAction(self, event):
	newDataSet = system.dataset.toPyDataSet(self.custom.LowLevelDS)
	ITEMNMBR = self.props.selection.data[0].ITEMNMBR
	sp = event.value
	rowIndex = 0
	
	
	for ndx, row in enumerate(newDataSet):
		if row["ITEMNMBR"] == ITEMNMBR:
			newDataSet = system.dataset.setValue(newDataSet, ndx, 'LowLevelSP', sp)
	
	
	self.custom.LowLevelDS = newDataSet
	
	self.getSibling("Table_3").refreshBinding("props.data")

```

```python

def transform(self, value, quality, timestamp):
	pds = system.dataset.toPyDataSet(value)
	columnNames = pds.columnNames
	ret = [
		{
			col: row[col]
			for col in columnNames
		} for row in pds
	]
	
	output = []
	for row in ret:
		newRow = {}
		bgColor = "pink" if row['QTYONHND'] < row['LowLevelSP'] else ""
		weight = "bold" if row['QTYONHND'] < row['LowLevelSP'] else ""
		for col in row:
				if col == "QTYONHND":
						newRow[col] = {"value": row[col], "style":{"background-color":bgColor,"font-weight":weight}}
				else:
						newRow[col] = row[col]
		output.append(newRow)
	return output

```

---

<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:** [March 26, 2025, 1:38pm UTC](https://forum.inductiveautomation.com/t/dataset-looping-through-how-to-get-current-rowindex-while-in-loop/104197/4 "2025-03-26T13:38:53Z")

</div>

Doesn't change much, but I'd write this part:

```python
for ndx, row in enumerate(newDataSet):
	if row["ITEMNMBR"] == ITEMNMBR:
		newDataSet = system.dataset.setValue(newDataSet, ndx, 'LowLevelSP', sp)

```

like this:

```python
row = ds.getColumnAsList(ds.getColumnIndex('ITEMNBR')).index(ITEMNBR)
data = system.dataset.setValue(ds, row, 'LowLevelSP', sp)

```

That said, if you're gonna end up with json in the end, it might be easier to start with json instead:

```python
headers = ds.columnNames
data = [dict(zip(headers, row)) for row in ds]
row = next(row for row in data if row['ITEMNBR'] == search_term)
row['LowLevelSP'] = sp

```

```python
def transform(self, value, quality, timestamp):
	for row in value:
		row['QTYONHND'] = {
			'value': row['QTYONHND'],
			'style': {
				'background-color': "pink" if row['QTYONHND'] < row['LowLevelSP'] else "",
				'font-weight': "bold" if row['QTYONHND'] < row['LowLevelSP'] else ""
			}
		}
	return value

```

Also note that if the table's `data` prop is bound to `custom.LowLevelDS`, you don't need to refresh it manually with `refreshBinding`, it will update automatically when it source data (your custom prop) is updated.
