# Create list from a bigger list

**URL:** <https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980>\
**Category:** Ignition\
**Created:** [November 15, 2019, 2:18am UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980 "2019-11-15T02:18:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ematschke](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ematschke/32/7544_2.png) [@ematschke](https://forum.inductiveautomation.com/u/ematschke)\
**Post date:** [November 15, 2019, 2:18am UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/1 "2019-11-15T02:18:54Z")

</div>

Is there a simple way to make a new partial list from an existing bigger list, but in a different order?  
I have

```auto
myList = ["blue", "red", "green", "orange"]

```

if If try

```auto
myNewList = myList[0:2]
print myNewLIst 

```

blue, red

how do i get blue and green  
trying not to do

```auto
myNewList = [myList[0]]
myNewList.append([myList[2])

```

this is a simplified example of much bigger lists.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [November 15, 2019, 3:27am UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/2 "2019-11-15T03:27:30Z")

</div>

This would return blue and green from your example:

```auto
myList = ["blue", "red", "green", "orange"]
# Extract every 2nd item from first list.
myNewList = myList[0::2]
print myNewList

```

But I'm guessing you're not looking for every second item.

> [@ematschke](#):
>
> this is a simplified example of much bigger lists

What kind of criteria/pattern would you use to select items from the old list for the new list?

---

<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 15, 2019, 12:45pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/3 "2019-11-15T12:45:03Z")

</div>

If you have another list containing the indices you want, you can use a list comprehension:

```auto
myList = ["blue", "red", "green", "orange"]
mySubscripts = [0, 2]
myNewList = [myList[x] for x in mySubscripts]

```

You should be aware that you were mis-using both slicing and the .append() method in your OP, yielding a list within a list in both cases. Spend some time in the python docs for [sequence types](https://docs.python.org/2.7/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) for the details.

---

<div class="post-metadata">

**Author:** ![ematschke](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ematschke/32/7544_2.png) [@ematschke](https://forum.inductiveautomation.com/u/ematschke)\
**Post date:** [November 15, 2019, 3:06pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/4 "2019-11-15T15:06:54Z")

</div>

Correct. The source list contains 14 items, but I want to pull out five of them in the order 2,9,3,0,1 so my SQL query will return items in the order I want to display in a table component.  
my other option was

```auto
vals = myList[2]
vals.append(myList[9])
vals.append(myList[3])
vals.append(myList[0]) or optionally vals.append(myList[0:2])
vals.append(myList[1])

```

I think that is correct now  
I was hoping there was a simple single line way to do it

As @pturmel pointed out, my OP was incorrect. I wrote that out as I was trying to leave the office.  
I guess I should also figure out how to post code snippets properly as you all have done.

---

<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 15, 2019, 3:27pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/5 "2019-11-15T15:27:59Z")

</div>

> [@ematschke](#):
>
> I think that is correct now

Nope. Missing brackets and errors with .append() still present. Starting here:

```auto
vals = myList[2]
vals.append(myList[9])

```

`vals` is not a list. So `.append()` won't work the way you think. (With the above, it is a string, so `.append()` is expected to add a character to it.)

And the latter `.append()` below, if `vals` was actually a list instead of a string:

```auto
vals.append(myList[0]) or optionally vals.append(myList[0:2])

```

is adding a slice of `myList` (which is a two-element list) as a single element of `vals`, nesting it. It would yield something like `[a, [b, c]]`.

Please, read up on how brackets work in python in the linked documentation.

---

<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 15, 2019, 3:31pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/6 "2019-11-15T15:31:27Z")

</div>

> [@ematschke](#):
>
> I guess I should also figure out how to post code snippets properly as you all have done.

Place a line with just three backquote characters above your code, and again below your code. Like so:  
```  
`# Some code`  
```

You can edit your prior posts to include these and they'll magically become readable.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [November 15, 2019, 3:47pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/7 "2019-11-15T15:47:23Z")

</div>

With that short list, you can one-line it like this (combining 2nd & 3rd lines of @pturmel's example):

```python
vals = [myList[x] for x in [2, 9, 3, 0, 1]]

```

In your latter example, your first line would need to look like this to make it a list (rather than a string):

```auto
vals = [myList[2]]

```

To get the expected result from this line:

> [@ematschke](#):
>
> vals.append(myList[0:2])

You'd need to replace `append` with `extend`.

---

<div class="post-metadata">

**Author:** ![ematschke](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ematschke/32/7544_2.png) [@ematschke](https://forum.inductiveautomation.com/u/ematschke)\
**Post date:** [November 15, 2019, 5:13pm UTC](https://forum.inductiveautomation.com/t/create-list-from-a-bigger-list/29980/8 "2019-11-15T17:13:05Z")

</div>

I think I get it  
This works

```auto
		selCols = project.globalVars.PART_STANDARDS_COLS[0:] # get all columns
		print selCols
		whereCols = [project.globalVars.PART_STANDARDS_COLS[0]] # Where column 0 and 2, . partNo and ViewID
		whereCols.append(project.globalVars.PART_STANDARDS_COLS[2]) # LAME
		print whereCols
		table = "PARTSTANDARDS"
		retValA,pyDataSet1 = project.db.dml.selectData(selCols,whereCols,whereVals,table,database) # don't need optional order or sort

```

and so does this, but for different reasons

```auto
		selCols = project.globalVars.PART_COORDS_COLS[0:] # get all columns, create new list
		print selCols # print result
		whereCols = project.globalVars.PART_COORDS_COLS[0:2] # Where column 0 and 1, . partNo and ViewID, create new list
		print whereCols # print result
		table = "PARTCOORDS" # table
		retValB,pyDataSet2 = project.db.dml.selectData(selCols,whereCols,whereVals,table,database) # don't need optional order or sort

```

it depends if the “:” is present in the list reference whether the result is a list or a string, among other things.  
The source lists are pre-defined in script modules
