Function Repository Resource:

Separate a list into two lists based on a given condition

Contributed by: Richard Hennigan (Wolfram Research)
 ResourceFunction["SelectDiscard"][list,crit] gives the pair {list1,list2} where list1 contains all elements ei of list for which crit[ei] is True and list2 contains the rest. ResourceFunction["SelectDiscard"][list,crit,n] limits list1 to n elements. ResourceFunction["SelectDiscard"][crit] represents an operator form of ResourceFunction["SelectDiscard"] that can be applied to an expression.

## Details

The list1 can be thought of as the "selected" elements, and list2 as the "discarded" elements.
The values ei in list2 do not necessarily give False for crit[ei]; they are simply the elements not included in list1.
The object list can have any head, not necessarily List.
When used on an Association, ResourceFunction["SelectDiscard"] picks out elements according to their values.
The order of elements returned in list1 and list2 are not guaranteed.
ResourceFunction["SelectDiscard"] can be used on SparseArray objects.

## Examples

### Basic Examples (5)

Separate elements that are even:

 In[1]:=
 Out[1]=

Use a pure function to test each element:

 In[2]:=
 Out[2]=

Select only the first expression that satisfies the condition:

 In[3]:=
 Out[3]=

Use the operator form of SelectDiscard:

 In[4]:=
 Out[4]=

SelectDiscard operates on values in an Association:

 In[5]:=
 Out[5]=

### Scope (6)

SelectDiscard gives elements for which applying the criterion explicitly yields True in the first list and everything else in the second:

 In[6]:=
 Out[6]=

Applying the criterion to the symbolic object x does not explicitly yield True:

 In[7]:=
 Out[7]=

Find pairs containing x:

 In[8]:=
 Out[8]=

Find up to 2 pairs containing x:

 In[9]:=
 Out[9]=

Fewer than the requested elements may be returned:

 In[10]:=
 Out[10]=

Use an operator form as the selection criterion:

 In[11]:=
 Out[11]=

 In[12]:=
 Out[12]=

Separate arguments in Unevaluated code:

 In[13]:=
 Out[13]=

### Generalizations & Extensions (4)

 In[14]:=
 Out[14]=

 In[15]:=
 Out[15]=
 In[16]:=
 Out[16]=

The result includes a list since only one can be sparse:

 In[17]:=
 Out[17]=

Compare to Select:

 In[18]:=
 Out[18]=
 In[19]:=
 Out[19]=

Separate a Dataset:

 In[20]:=
 Out[20]=
 In[21]:=
 Out[21]=

Separate a Graph by testing its vertices:

 In[22]:=
 Out[22]=

### Applications (5)

Select numbers up to 100 that equal 1 modulo both 3 and 5:

 In[23]:=
 Out[23]=

Select 4-tuples that read the same in reverse:

 In[24]:=
 Out[24]=

Select eigenvalues that lie within the unit circle:

 In[25]:=
 Out[25]=

Separate built-in Wolfram Language symbols based on whether their names contain less than 10 characters:

 In[26]:=
 In[27]:=
 Out[27]=
 In[28]:=
 Out[28]=

Select numeric quantities from a product:

 In[29]:=
 Out[29]=

### Properties & Relations (7)

The lengths of list1 and list2 will always sum to the length of the original list:

 In[30]:=
 Out[30]=

When specifying a maximum, the second list can contain elements that satisfy the given condition:

 In[31]:=
 Out[31]=

SelectDiscard is similar to TakeDrop except it takes/drops elements using the given function:

 In[32]:=
 Out[32]=
 In[33]:=
 Out[33]=
 In[34]:=
 Out[34]=

For crit that always return True or False, SelectDiscard is very similar to GatherBy:

 In[35]:=
 Out[35]=
 In[36]:=
 Out[36]=

However, the first list returned by SelectDiscard always corresponds to elements ei where crit[ei] gives True:

 In[37]:=
 Out[37]=
 In[38]:=
 Out[38]=

Similar results can be obtained with GroupBy:

 In[39]:=
 Out[39]=

 In[40]:=
 Out[40]=

GroupBy does not:

 In[41]:=
 Out[41]=

 In[42]:=
 Out[42]=
 In[43]:=
 Out[43]=

Compare with GatherBy and GroupBy:

 In[44]:=
 Out[44]=
 In[45]:=
 Out[45]=
 In[46]:=
 Out[46]=
 In[47]:=
 Out[47]=

Similar results can be obtained with a combination of Select and DeleteElements:

 In[48]:=
 In[49]:=
 Out[49]=
 In[50]:=
 Out[50]=

SelectDiscard tends to perform better with large lists:

 In[51]:=
 In[52]:=
 Out[52]=
 In[53]:=
 Out[53]=

The first list returned by SelectDiscard[list,crit] is equivalent to what's returned by Select[list,crit]:

 In[54]:=
 In[55]:=
 Out[55]=

The second list is effectively equivalent to Select[list,Not@*TrueQ@*crit]:

 In[56]:=
 Out[56]=

### Possible Issues (1)

When specifying a limit, the second list can contain elements ei where crit[ei] is True:

 In[57]:=
 Out[57]=

## Version History

• 1.0.0 – 05 July 2022