I’ve tried a few things but I’ve concluded that I would need to know how the block works (what system is uses to get its results) to be able to fix it further
For a more general approach
I made a function that groups all items by result of a function applied to each item.
E.g. it can divide a list of numbers into a list of primes and one of non-primes:
This is the implementation code
It will determine the result of the function only once for each item, and no (relatively inefficient) CONTAINS operation is required.
Update
I found a library block doing the same thing as my GROUP BY function (but slightly better); unsurprisingly it's called ; it's in the Frequency Distribution Analysis library.