How can I delete items from a list?

Technically, you can’t delete items from a list. You must create a new list excluding the refused items. Depending on your needs, you can do it positionally, eg:

Delete first item of a list:

set theList to {1, 2, 3}

rest of theList --> {2, 3}
items 2 thru -1 of theList --> {2, 3}

Delete last item of a list:

reverse of rest of reverse of theList --> {1, 2}
items 1 thru -2 of theList --> {1, 2}

Delete item 4 of a list:

set theList to {1, 2, 3, 4, 5, 6, 7, 8}

items 1 thru 3 of theList & items 5 thru -1 of theList --> {1, 2, 3, 5, 6, 7, 8}

If you don’t know the index of the item in the list, but only its contents, you must iterate through the entire list searching for the related item. You can delete the first ocurrence, first two ocurrences, any coincidence of the item… What you need. A quick sample:

Delete certain items from a list:

set theList to {1, 2, 3, 4, 5, 6, 7, 8, "Michael Jordan", {a:2}}

set itemsToDelete to {4, "Michael Jordan", {a:2}}

set cleanList to {}

repeat with i from 1 to count theList
	if {theList's item i} is not in itemsToDelete then set cleanList's end to theList's item i
end repeat

cleanList --> {1, 2, 3, 5, 6, 7, 8}

Depending on the nature of the data you are manipulating, you can invent new and faster ways to delete items. For example, if you have a list of strings you can use this tricky method, which can be much faster than iterating thru all items in a very long list:

set theList to {"ab", "bc", "cd", "de"}

set itemsToDelete to {"bc", "de"}

set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "TRICK"
set theList to "TRICK" & theList & "TRICK"
--> theList = "TRICKabTRICKbcTRICKcdTRICKdeTRICK"
repeat with i in itemsToDelete
	set AppleScript's text item delimiters to "TRICK" & i & "TRICK"
	set theList to theList's text items
	set AppleScript's text item delimiters to "TRICK"
	set theList to theList as text
end repeat

set AppleScript's text item delimiters to "TRICK"
--> (theList's text 6 thru -6) = "abTRICKcd"
set theList to (theList's text 6 thru -6)'s text items
set AppleScript's text item delimiters to prevTIDs

theList --> {"ab", "cd"}

(Of course you can use a very original string instead of “TRICK”, just in case “TRICK” is contained in the original list, as “((ASCII character 0) & (ASCII character 1) & (ASCII character 256))” or a similar unfrequent string).

Take a look to our Scripting Additions section for some osaxen able to manipulate lists, such as ACME Script Widgets or XTool.