Removing Pairs from a List

I recently had to pare down a list of pairs of items having identified either of the members of the pair. This script does it, and might be useful to others. I didn’t pursue it to the point of accepting a list of items to be pared, but that would make it faster than these multiple calls.

set pairsToPare to {"A", 1, "B", 2, "C", 3, "D", 4, "E", 5}

RemPairs(RemPairs(RemPairs(pairsToPare, 2), "E"), "1")
--> {"C", 3, "D", 4}

on RemPairs(OrigList, TakeOut) -- To be done by pairs, TakeOut can be either one.
	-- Set up Properties using a Script Object in case the list is long
	script p
		property Original : OrigList
		property inList : {}
		property OutList : TakeOut
	end script
	-- Do the removal in pairs
	repeat with i from 1 to (count of (p's Original)) - 1 by 2
		set oddItem to ((p's Original)'s item i)
		set evenItem to ((p's Original)'s item (i + 1))
		tell p's OutList
			if (evenItem is not in it and oddItem is not in it) then
				set end of p's inList to oddItem
				set end of p's inList to evenItem
			end if
		end tell
	end repeat
	return p's inList
end RemPairs

You might be surprised to know that your function can already handle a list as input. Also, I’m not sure if you used “1” on purpose, but it works since:

1 is in "1" --> true
Anyway, I’ve tried to condense your script slightly:

set theList to {"A", 1, "B", 2, "C", 3, "D", 4, "E", 5}
removeItemPairs(theList, {2, "E", 1})
--> {"C", 3, "D", 4}

on removeItemPairs(inputList, removeList)
    set newList to {}
    repeat with i from 1 to count of inputList by 2
        tell inputList to tell {item i, item (i + 1)} to if beginning is not in removeList and end is not in removeList then
            set end of newList to beginning
            set end of newList to end
        end if
    end repeat
    newList
end removeItemPairs

I’m not really sure on how to use script objects with properties yet, so I’ve excluded them.

Querty;

Here’s your script (thank you for it) with script properties added to it. The advantage is negligible for tiny lists like the example, but if the list involves a lot of entries (I use it for a large calendar manipulation involving events and dates), then using script objects speeds it up remarkably because of the differences between the ways the system handles script objects vs. simple lists.

set theList to {"A", 1, "B", 2, "C", 3, "D", 4, "E", 5}
removeItemPairs(theList, {2, "E", 1})
--> {"C", 3, "D", 4}

on removeItemPairs(inputList, removeList)
	
	script p
		property iL : inputList
		property rL : removeList
		property nL : {}
	end script
	----
	repeat with i from 1 to count of p's iL by 2
		tell p's iL to tell {item i, item (i + 1)} to if beginning is not in p's rL and end is not in p's rL then
			set end of p's nL to beginning
			set end of p's nL to end
		end if
	end repeat
	return p's nL
end removeItemPairs