Create list from a bigger list

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

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

if If try

myNewList = myList[0:2]
print myNewLIst 

blue, red

how do i get blue and green
trying not to do

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

this is a simplified example of much bigger lists.

This would return blue and green from your example:

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.

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

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.

2 Likes

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[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.

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

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:

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.

1 Like

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.

1 Like

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

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

vals = [myList[2]]

To get the expected result from this line:

You'd need to replace append with extend.

2 Likes

I think I get it
This works

		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

		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

1 Like