# Lookup or find function in script

**URL:** <https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914>\
**Category:** Ignition\
**Created:** [January 25, 2012, 10:52pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914 "2012-01-25T22:52:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimE](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TimE](https://forum.inductiveautomation.com/u/TimE)\
**Post date:** [January 25, 2012, 10:52pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/1 "2012-01-25T22:52:57Z")

</div>

Background:  
I started with a dataset with about 8000 rows of xy data. After a lot of calculations I now have a resulting dataset of about 36 x-axis values representing the locations of peaks and valleys of the original dataset.

What I want to do is take each x-axis value and find the cooresponding y-axis value from the original dataset.

I am doing all of this in scripting. How do I search for a value in a dataset or py dataset?

Thanks,

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [January 26, 2012, 12:13am UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/2 "2012-01-26T00:13:46Z")

</div>

Unfortunately, you will have to loop through the original dataset looking for the value. There is no special scripting function for that.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 26, 2012, 10:33am UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/3 "2012-01-26T10:33:43Z")

</div>

Would using a dictionary help? I haven’t played with them myself, but I seem to remember reading about lookups in dictionaries being faster than iterating through lists…

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [January 26, 2012, 4:56pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/4 "2012-01-26T16:56:11Z")

</div>

Well, yes but you would have to iterate through the dataset to create the dictionary. So either way you have to perform the loop.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [January 27, 2012, 12:02pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/5 "2012-01-27T12:02:23Z")

</div>

Yes, but if he’s already iterated through it once to get his “36 x-axis values”, why not create the dictionary at the same time?

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [January 27, 2012, 4:23pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/6 "2012-01-27T16:23:42Z")

</div>

You are absolutely right. I missed that. A dictionary does allow faster searching. Here is an example of using a dictionary:[code]dict = {}  
dict[“key1”] = [arrayVal1, arrayVal2, …]  
dict[“key2”] = [arrayVal1, arrayVal2, …]

print dict.has\_key(“key1”)  
print dict.get(“key1”, defaultvalue) # default value can be empty list or None[/code]

---

<div class="post-metadata">

**Author:** ![TimE](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TimE](https://forum.inductiveautomation.com/u/TimE)\
**Post date:** [February 22, 2012, 9:27pm UTC](https://forum.inductiveautomation.com/t/lookup-or-find-function-in-script/3914/7 "2012-02-22T21:27:29Z")

</div>

I have been without my computer for about 3 weeks and I was given another project to work on. I am not very familiar with dictionaries either. This is what is working for me.

`def findYvalue(ds1, ds2):
	import system
	pyPVData = system.dataset.toPyDataSet(ds1)
	pyRollData = system.dataset.toPyDataSet(ds2)
	headers = ["distance", "height"]
	newData = []
	for xPV in pyPVData:
		for xRoll in pyRollData:
			if xPV["distance"] == xRoll["distance"]:
				newData.append([xRoll["distance"], xRoll["height"]])
	dsGaugeBands = system.dataset.toDataSet(headers, newData)
	return dsGaugeBands`

Thank you,
