# Python list to dataset

**URL:** <https://forum.inductiveautomation.com/t/python-list-to-dataset/6129>\
**Category:** Ignition\
**Created:** [April 1, 2013, 4:03pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129 "2013-04-01T16:03:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 1, 2013, 4:03pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/1 "2013-04-01T16:03:12Z")

</div>

I have a list

```auto
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…

---

<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:** [April 1, 2013, 4:22pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/2 "2013-04-01T16:22:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 1, 2013, 5:32pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/3 "2013-04-01T17:32:52Z")

</div>

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:

```auto
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?

---

<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:** [April 1, 2013, 5:41pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/4 "2013-04-01T17:41:32Z")

</div>

Does this work?

```auto
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
```

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 1, 2013, 5:45pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/5 "2013-04-01T17:45:04Z")

</div>

…thanks kevin  
You caught my mistake.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 17, 2018, 2:29pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/6 "2018-11-17T14:29:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 17, 2018, 4:08pm UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/7 "2018-11-17T16:08:49Z")

</div>

Kevin’s first try was the closest:

```auto
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.

---

<div class="post-metadata">

**Author:** ![nnaik](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@nnaik](https://forum.inductiveautomation.com/u/nnaik)\
**Post date:** [April 25, 2022, 11:31am UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/8 "2022-04-25T11:31:57Z")

</div>

You are the Best, Sir !

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [November 2, 2022, 3:18am UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/9 "2022-11-02T03:18:12Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/9/99607299a389d3b9e972a69d2065eb4c4597be54.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 2, 2022, 3:23am UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/10 "2022-11-02T03:23:28Z")

</div>

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:

```auto
[ <-- 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

```auto
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']`

---

<div class="post-metadata">

**Author:** ![Lalillo1](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Lalillo1](https://forum.inductiveautomation.com/u/Lalillo1)\
**Post date:** [November 2, 2022, 5:51am UTC](https://forum.inductiveautomation.com/t/python-list-to-dataset/6129/11 "2022-11-02T05:51:40Z")

</div>

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])

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