# Using dict comprehension to append data

**URL:** <https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [August 1, 2023, 3:00pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225 "2023-08-01T15:00:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:00pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/1 "2023-08-01T15:00:23Z")

</div>

does Ignition not support comprehension? this isn't working, but it's valid code:

```python
#
#	'value' is the entire dataset, we only want the assetName at this point
#
assetNames = {}
py_value = system.dataset.toPyDataSet(value)
for item in py_value: # ↓ column ↓ data
	assetNames = { **assetNames,** {'assetName':item['assetName']}}
return [assetNames]	#<--- cuz everything needs to be a list... :)

```

i am building a table but only require one column from the dataset to populate the table. this should return a dict of key:value pairs (column and data)... but it isn't.

AFAIK, the table parses a data object of key:value pairs; the key is the column name, the value is, well, the value. this should deliver what the table parser is looking for.

okay. rant over. how do i fix this dang script?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 1, 2023, 3:05pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/2 "2023-08-01T15:05:15Z")

</div>

Doesn't work in Python 2, I'm pretty sure.

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:05pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/3 "2023-08-01T15:05:47Z")

</div>

fffffuuuu.... i keep forgetting that we aren't running Python 3. sorry.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 1, 2023, 3:07pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/4 "2023-08-01T15:07:38Z")

</div>

Specifically, the issue is multiple sets of kwargs to the dict comprehension in one go.

> <https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python/26853961#26853961>

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:11pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/5 "2023-08-01T15:11:57Z")

</div>

the issue is how to do it _ **in Python 2** _. 😃 because i haven't used 2 for about a decade...  
EDIT: ah... yes. great post. thank you.

---

<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:** [August 1, 2023, 3:28pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/6 "2023-08-01T15:28:53Z")

</div>

What were you trying to do exactly ? Make a list of dicts where the keys are the columns names and the values are the values, for each row ?

Something like this ?

```python
headers = value.columnNames
return [dict(zip(headers, row)) for row in system.dataset.toPyDataSet(value)]

```

But maybe with only specific column/keys ?

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:30pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/7 "2023-08-01T15:30:55Z")

</div>

okay. read the whole post (very good). but i hit a hiccup: i originally had tried this:

```python
assetNames = {}
py_value = system.dataset.toPyDataSet(value)
for item in py_value:
	assetNames.update({'assetName':item['assetName']})
return [assetNames]	#<--- cuz everything needs to be a list... :P

```

which, ironically, is EXACTLY what the SO post explained (so i'm not actually an idiot after all...). but it only returned a _single_ element (the LAST element): `[{assetName: 70456-20}]` so i went the comprehension route. so i'm not sure what's going on here.

---

<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:** [August 1, 2023, 3:32pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/8 "2023-08-01T15:32:14Z")

</div>

Well for each item in the dataset, it will update the same key in your dict, over and over again. So in the end you only have the last one.

---

<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 1, 2023, 3:34pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/9 "2023-08-01T15:34:18Z")

</div>

He's trying to populate a data property of a table in perspective. So I believe he may actually want a list of dictionaries in that case right?

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:36pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/10 "2023-08-01T15:36:24Z")

</div>

i was, as i mentioned, trying to build a table but only using ONE of the columns of data. so i don't know if your method would work, but i'll give it a shot with an explicit`header` definition and see what happens. i am not familiar with the `dict` method or what it does... so some reading first??? nah. slam it in there!! learn from the pieces!! 😃

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:36pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/11 "2023-08-01T15:36:46Z")

</div>

yes. that's correct.

---

<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 1, 2023, 3:39pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/12 "2023-08-01T15:39:07Z")

</div>

Think you may want something like this instead then  
`return [{"assetName":row["assetName"]} for row in system.dataset.toPyDataSet(value)]`

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:39pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/13 "2023-08-01T15:39:51Z")

</div>

> [@pascal.fragnoud](#):
>
> it will update the same key in your dict, over and over again

🤦‍♂️ I HATE PYTHON 2...

---

<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:** [August 1, 2023, 3:39pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/14 "2023-08-01T15:39:55Z")

</div>

If the key is always the same, then it HAS to be a list of dicts.

@ian.sebryk : doing things in just one expression is my jam, my peanut butter, my cheese and beer all combined.

```python
return [{column_name: val} for val in ds.getColumnAsList(ds.getColumnIndex(column_name))]

```

> [@ian.sebryk](#):
>
> 🤦‍♂️ I HATE PYTHON 2...

It does the exact same thing in python 3 though.

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:41pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/15 "2023-08-01T15:41:01Z")

</div>

> [@pascal.fragnoud](#):
>
> It does the exact same thing in python 3 though.

but i haven't used `update` in forever because i just use comprehensions... 😛

EDIT: jam, peanut butter, cheese, and beer... now i'm hungry. 😃

---

<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:** [August 1, 2023, 3:42pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/16 "2023-08-01T15:42:06Z")

</div>

You can use them in python 2 as well. No issue here.

try the line I posted above.

And if you want to update a dict in just one expression without having to use update, which is a pain to use in comprehension anyway because it doesn't return the updated dict:

```python
new_dict = dict(new_key=some_value, **old_dict)

```

this can be used in comprehension easily

```python
[dict(k=v, **old_dict) for k, v other_dict.items()]

```

> [@ian.sebryk](#):
>
> dict comprehensions didn't exist until Python 3

```python
{k:v for k, v in dict.items()}

```

absolutely works in python 2. What do you call comprehension ?

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:46pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/17 "2023-08-01T15:46:13Z")

</div>

> [@pascal.fragnoud](#):
>
> You can use them in python 2 as well

dict comprehensions didn't exist until Python 3. which is why my original code didn't work: we're running Python 2.

at any rate, it doesn't work, so i'm trying your code now... stdby. 🕙

EDIT: \*_boop\*_ that worked.

---

<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 1, 2023, 3:48pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/18 "2023-08-01T15:48:41Z")

</div>

You may not be able to do them with the `**someDict` but you can certainly make dicts on the fly with comprehensions as @paul-griffith did in this post , kind of related as well with going from a ds to a dict

> [@PyDataset to Dictionary](https://forum.inductiveautomation.com/t/pydataset-to-dictionary/38968/6):
>
> I mean, as long as we’re golfing, you can also nest the comprehensions: NewDict = { 'Data %s' % i: { colName: value for colName, value in zip(pyData.columnNames, list(row)) } for i, row in enumerate(pyData) }

Though maybe this counts as a list comprehension, but if the final result is a dict, its a dict comprehension in my book lol.

---

<div class="post-metadata">

**Author:** ![ian.sebryk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ian.sebryk/32/49319_2.png) [@ian.sebryk](https://forum.inductiveautomation.com/u/ian.sebryk)\
**Post date:** [August 1, 2023, 3:56pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/19 "2023-08-01T15:56:31Z")

</div>

okay. so what i'm learning here is:

- data visualization tools in Ignition scripting still suck (Python → prettyPrint, c'mon)
- i need more prayers to God that my higher-ups let me implement a better IDE strategy soon
- → **i don't need to convert everything to py\_dataset** ←
- i may actually commit seppuku before i get my head around this environment

thank you for all your help and comments. very helpful at keeping the blade at bay. 😉

EDIT: i've always had difficutly visualizing anything, period. once it does click, though, it stays forever. so please bear with as i keep trying to 'see' how this stuff hangs together. very irritating disability for a coder... meditation? forget it.

---

<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 1, 2023, 4:06pm UTC](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225/20 "2023-08-01T16:06:44Z")

</div>

Regarding pretty printing of (assuming) dictionaries hopefully eases your pain a bit, I keep this in a project library so I can call it when needed.

```auto
import pprint

mydict = {'nest0':'hi',
		'nest1':{'something':1},
		'nest2':{'nest1again':{'somethingelse':'bye'}}
		}

pp = pprint.PrettyPrinter(depth=4)
pp.pprint(mydict)

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/f/ef6eff3cc380dab1ac6a5e7f91cfd48017372ca8.png)

> <https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries>

In my experince PyDataSets are most useful when you want access to all or almost all columns in a dictionary. So for things like

```auto
for (someCol1, someCol2, someCol3) in system.dataset.toPyDataSet(ds):
    # do something with your extracted columns
    # don't need to fetch them via col1=row['someCol1']
    # or via col1 = ds.getValueAt(row,'someCol1') if a basic dataset

```

it's very useful.

[Next page](https://forum.inductiveautomation.com/t/using-dict-comprehension-to-append-data/77225.md?page=2)
