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.

11 Likes

You are the Best, Sir !

Hi @pturmel,
I am trying to follow your example. Now, in this case, how do you convert the 'spam' into Dataset [2R x 3C] ?

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

Or the following into [3R x 3C], and so on?

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

As you can see in the next screenshot, I am not getting the desired result:

The [[x] for x in spam] in your case is encasing your lists in spam into deeper lists, e.g. a list of lists of lists. This is the outcome of that list comprehension:

[ <-- list
  [ <-- of lists (this is what is trying to be written into the column in the dataset)
    ['apples', 'oranges', 'HELLO WORLD'] <-- of lists
  ],
  [
    ['apples', 'oranges', 'HELLO WORLD']
  ],
  [
    ['apples', 'oranges', 'HELLO WORLD']
  ]
]

You can't write a list to a cell value in a dataset.
For your case, you don't need the [[x] for x in spam] part, just use spam AND you also need to add another 2 columns since you have three columns in your lists

ds = system.dataset.toDataSet(['Green* Fruit', 'Orange Fruit', 'Code Output Text'], spam)

*Green fruit because Granny Smith is the king of apples (raw and cooked)

In the original example, spam = ['apples', 'oranges', 'HELLO WORLD']

1 Like

Thank you very much for your great help. Understood now. This was also a great example for me too:

##This example create a single column dataset.
headers = ['col1', 'col2', 'col3']
data = [[1,2,3], [4,5,6], [7,8,9]]
ds = system.dataset.toPyDataSet(system.dataset.toDataSet(headers, data))
listOfRowValues=
for row in ds:
listOfRowValues.append(row[0])
print (row[0],row[1],row[2])