# Looping Through a PyDataset

**URL:** <https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291>\
**Category:** General Discussion\
**Created:** [July 13, 2023, 3:39am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291 "2023-07-13T03:39:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [July 13, 2023, 3:39am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/1 "2023-07-13T03:39:03Z")

</div>

Hi, I have a dumb question...  
I have this dataset on a custom value property called "test"  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/0/d04e1b72ed5b4779df7c76690cdbbdb8fbb5ffde.png)

I would like to retrun only the value "B1" from the only one colum named "parts"  
And this is what I have attempted in a Script Transoform...but it only returns the vavlue from row 1, column 0 which is IE100.13

```auto
	# Convert to a PyDataset
	pyData = system.dataset.toPyDataSet(value)
	 
	# Use a for loop to extract out a single row at a time
	for row in pyData:
	    # Use either the column index or the column name to extract a single value from that row.
	    val = row[0]
	return val

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/5/85e57eca58615193d7ef383e7315a921203bc120.png)

I honestly do not understand it yet... how would only return the value of row 0, column 0 which is "B1" ?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 13, 2023, 3:40am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/2 "2023-07-13T03:40:13Z")

</div>

```auto
.
.
.
return pyData[0][0]

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 13, 2023, 3:50am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/3 "2023-07-13T03:50:54Z")

</div>

> [@Lalillo1](#):
>
> ```auto
> for row in pyData:
> # Use either the column index or the column name to extract a single value from that row.
> val = row[0]
> 
> ```

This will iterate through each row in the dataset, writing `val` to the row's first column's value each time. Once it ends, `val` will therefore = the last row's first column's value

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [July 13, 2023, 3:58am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/4 "2023-07-13T03:58:46Z")

</div>

Thank you very much for the explanation... and how can I adjust this for loop, so that it only returns "B1" value?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 13, 2023, 4:05am UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/5 "2023-07-13T04:05:24Z")

</div>

Exactly how Kevin answered.

You don't need a loop if you know the index that you want to return.  
My question is though, what's the point of the code? You could just as easily (and far more efficiently) return this using an expression binding with:

```auto
{this.custom.test}[0,0]
// or possibly just
{this.custom.test}[0]

```

If you can do something without script, you should always do it that way. Scripting adds an additional layer of load and hence reduces scalability and increases resource requirements on the server.

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [July 13, 2023, 4:08pm UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/6 "2023-07-13T16:08:46Z")

</div>

> [@nminchin](#):
>
> as easily (and far more efficiently) return this using an expression binding with:

Thank you. this is just for learning purposes... What if I have a 2x2 dataset (row0,row1 x col0, col1)? .. How would I return a value for this two cases:. the value of (row0,col0) and the value of (row1,col1) ? using a the for loop?  
I was following this example from the Ignition User Manual:  
[https://docs.inductiveautomation.com/display/DOC81/Datasets](https://docs.inductiveautomation.com/display/DOC81/Datasets)

```auto
# Convert to a PyDataset
pyData = system.dataset.toPyDataSet(cities)
 
# Use a for loop to extract out a single row at a time
for row in pyData:
    # Use either the column index or the column name to extract a single value from that row.
    city = row[0]
    population = row["Population"]
    print city, population

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 13, 2023, 4:13pm UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/7 "2023-07-13T16:13:55Z")

</div>

Why are you trying to use a loop when you know the address?

You wouldn't knock on the door of every house on the street to see if it was Sid's house if you already knew that Sid was in number 17, would you? You'd just go directly to number 17.

The example you have quoted is performing an action on every row of the dataset. In your question you aren't doing this.

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [July 13, 2023, 4:18pm UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/8 "2023-07-13T16:18:47Z")

</div>

Understood now. Thank you all for the education. Many thanks.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 13, 2023, 5:18pm UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/9 "2023-07-13T17:18:24Z")

</div>

[w3schools.com](http://w3schools.com) has some very easy to read and use tutorials on lists and dictionaries. I've linked to the start pages. Use the menu on the left of the page to go through the various sections. Getting this clear in your head at the start will make life much easier. (I wish I had!)

> **[Python Lists](https://www.w3schools.com/python/python_lists.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

> **[Python Dictionaries](https://www.w3schools.com/python/python_dictionaries.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

Invest some time in working through the exercises.

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [July 13, 2023, 8:01pm UTC](https://forum.inductiveautomation.com/t/looping-through-a-pydataset/76291/10 "2023-07-13T20:01:14Z")

</div>

Thank you very much Transistor!
