Python list to dataset

I have a list

spam = ['apples', 'oranges', 'HELLO WORLD']

I am trying to coerce this to a dataset with each item being a different row and only 1 column.
Not having any luck…

spam = ['apples', 'oranges', 'HELLO WORLD']
ds = system.dataset.toDataSet(['Column Name'], [spam])

In the script module I have this function

def list(): import system listNumber = system.tag.read('tag').value lists = [] for x in range(listNumber): oneRow = "list " + str(x + 1) lists.append(oneRow) ds = system.dataset.toDataSet(['Column name'],[lists]) return ds

Then I have a dataset custom property on a window that is bound to this call:

runScript("app.Winchester.Shift.listShifts()", 1000)

I keep getting errors like:
IndexError: Row 0 doesn’t have the same number of columns as header list.
… I am trying to bind dropdown selector data to this list.

How about taking this string
n = [‘apple’, ‘orange’, ‘blue’, ‘red’]
and converting it to a dataset with one column and these items as rows?
Would this be too involved?

Does this work?

def list():
   import system
   listNumber = system.tag.read('tag').value
   lists = []
   for x in range(listNumber):
      oneRow = ["list " + str(x + 1)]
      lists.append(oneRow)
   ds = system.dataset.toDataSet(['Column name'], lists)
   return ds

…thanks kevin
You caught my mistake.

If anyone else is struggling with this as I was for over an hour, note that lists.append requires oneRow to be passed as an [array] (or is it a list?). The square brackets are required.

1 Like

Kevin’s first try was the closest:

spam = ['apples', 'oranges', 'HELLO WORLD']
ds = system.dataset.toDataSet(['Column Name'], [[x] for x in spam])

Also, it’s a really bad idea to name a function ‘list’, as that hides the built-in list function in that scope.

7 Likes