# How to go from a dataset to a dictionary?

**URL:** <https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [August 8, 2019, 6:15pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281 "2019-08-08T18:15:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:15pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/1 "2019-08-08T18:15:18Z")

</div>

I have a dataset from a query binding and I want to turn it into a dictionary in a script. How can I do this? Ignition 7.9.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:23pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/3 "2019-08-08T18:23:59Z")

</div>

That’s only in 8.0 I believe, I don’t have that option.

---

<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:** [August 8, 2019, 6:30pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/4 "2019-08-08T18:30:26Z")

</div>

There’s no obvious mapping from dataset to dictionary, so you’ll need to tell us what your dataset looks like and what “shape” you want the resulting dictionary to be.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:30pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/5 "2019-08-08T18:30:58Z")

</div>

It’s two columns, I would like the first column to be the key and the second column to be the value.

---

<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:** [August 8, 2019, 6:31pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/6 "2019-08-08T18:31:35Z")

</div>

2 columns and 1 row or multiple rows?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:31pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/7 "2019-08-08T18:31:50Z")

</div>

It has multiple rows.

---

<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:** [August 8, 2019, 6:33pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/8 "2019-08-08T18:33:58Z")

</div>

So something like this?

```auto
{
"column 1 name" : [values, in, each, row, for, column 1],
"column 2 name" : [values, in, each, row, for, column 2]
}

```

where the keys are the column name, and the value is an array of the values from the rows of that column?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:35pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/9 "2019-08-08T18:35:50Z")

</div>

The dataset is such

```auto
id description
1 label1
2 label2
3 label3
...

```

and so on. I’d like the dictionary to be

```auto
{
   "1": "label1",
   "2": "label2",
   "3": "label3",
   ...
}

```

---

<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:** [August 8, 2019, 6:38pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/10 "2019-08-08T18:38:30Z")

</div>

Psuedocode-ish:

```auto
dict = {}

for row in dataset:
	dict[row['id']] = row['description']

```

You could also just use the indices, i.e. `row[0]` and `row[1]`.

and I think for the iteration to work it needs to be a PyDataSet, otherwise you’ll have to use some of the other more verbose methods to get the data out.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 8, 2019, 6:42pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/11 "2019-08-08T18:42:43Z")

</div>

That was it, thanks.

---

<div class="post-metadata">

**Author:** ![patrick.szalapski](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@patrick.szalapski](https://forum.inductiveautomation.com/u/patrick.szalapski)\
**Post date:** [May 22, 2025, 1:22pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/12 "2025-05-22T13:22:17Z")

</div>

Hope this helps, for one narrow kind of input as described:

```python
def to_dataset(array_of_objects):
	"""Returns a Dataset made from an array of PyDictionaries that have only key-value pairs--no children"""
	if array_of_objects == []: raise ValueError("Needs at least one item with keys. Cannot derive keys from an empty array.")
	rows = [item.values() for item in array_of_objects]
	return system.dataset.toDataSet(array_of_objects[0].keys(), rows)	

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 22, 2025, 1:26pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/13 "2025-05-22T13:26:38Z")

</div>

I've realized since this post (which is 6 years old ) I should just use PyDataSet each row is functionally a dictionary. You can simply do

```python
#all these return a pydataset now
data = system.db.runNamedQuery/runQuery/runPrepQuery(query) 
for row in data:
    # Access like a dictionary
    someValue = row['someVal']

```

Manually converting to a dictionary is wasting time and computation.

Or if you know your defined columns, then unpacking in the for loop

```python
for (column1, column2, ..., columnN) in data:
    # now we have access to the column data directly

```

---

<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:** [May 22, 2025, 1:35pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/14 "2025-05-22T13:35:44Z")

</div>

> [@patrick.szalapski](#):
>
> ```python
> rows = []
> for item in arrayOfObjects: rows.append(item.values())
> 
> ```

If you're gonna put the `append` on the same line, you might as well use a comprehension:

```python
rows = [item.values() for item in arrayOfObjects]

```

---

<div class="post-metadata">

**Author:** ![patrick.szalapski](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@patrick.szalapski](https://forum.inductiveautomation.com/u/patrick.szalapski)\
**Post date:** [May 22, 2025, 2:11pm UTC](https://forum.inductiveautomation.com/t/how-to-go-from-a-dataset-to-a-dictionary/27281/15 "2025-05-22T14:11:00Z")

</div>

> [@patrick.szalapski](#):
>
> ```python
> n arrayOfObjects: rows.append(item.values())
> return system.dataset.toDataSet(array
> 
> ```

Thanks, love it! I've modified my code above.
