If you have another list containing the indices you want, you can use a list comprehension:
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 for the details.
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
vals = myList
vals.append(myList) or optionally vals.append(myList[0:2])
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.
selCols = project.globalVars.PART_STANDARDS_COLS[0:] # get all columns
whereCols = [project.globalVars.PART_STANDARDS_COLS] # Where column 0 and 2, . partNo and ViewID
whereCols.append(project.globalVars.PART_STANDARDS_COLS) # LAME
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
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