Using dict comprehension to append data

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.


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?

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 explicitheader 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!! :smiley:

yes. that's correct.

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

:man_facepalming: I HATE PYTHON 2...

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.

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

It does the exact same thing in python 3 though.


but i haven't used update in forever because i just use comprehensions... :stuck_out_tongue:

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

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:

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

this can be used in comprehension easily

[dict(k=v, **old_dict) for k, v other_dict.items()]
{k:v for k, v in dict.items()}

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


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. :clock10:

EDIT: *boop* that worked.

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

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

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. :wink:

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.

1 Like

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.

import pprint

mydict = {'nest0':'hi',

pp = pprint.PrettyPrinter(depth=4)


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

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.

1 Like

bruh. i could kiss you. i won't, cuz that's gross, but the sentiment is there. :smiley: i tried something similar earlier, but i was trying from pprint import PrettyPrinter and it crapped out. so i assumed it just didn't work and i was stuck with this bing-bong IDE and NO visualization tools... very happy to be wrong.

1 Like

crazy question here, and maybe I am missing something important, but if all you want is a single column of a dataset, then why not just use system.dataset.filterColumns()?

return system.dataset.filterColumns(value,['assetName'])

Or even better would be to use an expression binding.

TIP: The columnRearrange() expression function can be used to filter dataset columns. It will only return the columns given in the expression in the order given.


The table component will work with a dataset just fine.


...well :poop:.

:laughing: time pressure. i want to do nothing but read docs and experiment, but i have to fix stuff sooner. thank you for this. i bow to your greater wisdom.

as for expressions, i'm just starting with that today... and it's not bending to my will just yet. of course i'm trying to do something obtuse and stupid with it, so that's likely why. :stuck_out_tongue:


NB: don't sleep on this advice. Expressions are way faster than scripting.


that's why i'm digging in to them today. our server is just hammered with my predecessor's spaghetti monster code. it's a bit overwhelming at times. like: recursing a function to a blank baseline just to get a single user dataset. :scream: this kind of thing is just peppered throughout 25+ projects. so optimizing as i go is a critical add. so getting my head around the new syntax is... fun. i don't want to deploy it just to deploy it, if you know what i mean. it has to have purpose that makes sense in the context of the massive refactoring i'm doing. so knowing when to use it is just slightly more important as how. but to get there i have to start with how.

my brain is a wonderland... just not the sexy one from the song... :bikini:

Ian, I think it is time for a serious talk with your boss(es).

You been citing this for weeks. It is more than worn out. Your bosses' desire for the impossible won't magically make it happen.

The drama you are bringing as a result started as amusing, but is now just tiresome. Please reduce the whining.

This whining, too.

1 Like


1 Like