# Filter lists by discarding sublists that contain integers different from 0 or 1

Imagine I have a list of the type

Lst = {{1,0,1,1},{4,0,1,1}, {1,1,0,0},{2,1,1,1},{0,0,0,1}}


I want to select only those sublists that only contain elements 0 or 1. For example for the above list the reduced list which I want is

IWantThis = {{1,0,1,1},{1,1,0,0},{0,0,0,1}}


Motivation for this is the given situation describes spinless fermions or spin 1/2 objects.

• If the order doesn't matter then BitAnd[Lst, 1] ⋂ Lst does it Feb 27, 2019 at 12:21

To filter a list

list = {{1, 0, 1, 1}, {4, 0, 1, 1}, {1, 1, 0, 0}, {2, 1, 1, 1}, {0, 0, 0, 1}};


you can use Select like this:

Select[list, ContainsOnly[{0, 1}]]
(* {{1, 0, 1, 1}, {1, 1, 0, 0}, {0, 0, 0, 1}} *)


This should work with Mathematica 10.2+.

• OK. Thanks a lot Feb 27, 2019 at 11:50
• In earlier versions, use Cases[] instead: Cases[list, {(0 | 1) ..}]. Mar 1, 2019 at 9:30

Using Select and Union.

Lst = {{1,0,1,1},{4,0,1,1}, {1,1,0,0},{2,1,1,1},{0,0,0,1}};
Select[Lst, Union[#] == {0, 1} &]


{{1,0,1,1},{1,1,0,0},{0,0,0,1}}

I have used Unionspecifically, because it sorts out the list after removing duplicates unlike DeleteDuplicates.

• Ok thanks a lot Feb 27, 2019 at 11:45