Maybe something like this ineqFu[a_, b_, max_, expr_, data_] := If[ a == 0, expr <= data[[b]] , If[ b == max, data[[a]] < expr , data[[a]] < expr <= data[[b]] ] ] max = Length@bounds + 1; tokenizedIneqs = ineqFu[##, max, token, r] & @@@ Partition[Range[0, max], 2, 1]; funkyTown = Which @@@ (Function@ Evaluate[Riffle[tokenizedIneqs, Range[max]]] /. token :> #[[2]]); result = GatherBy[s, funkyTown]; We then have result === output > True