Removing sublists containing elements from another list's sublist

Let there be two lists that have sublists:

a[] = {{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}}


and

b[] = { {1, 2}, {4, 5, 6}}


I want to remove all those sublists in a[] that has elements contained in a sublist of b[]. So after removal I am expecting

a[] = {{2, 3, 4, 4}, {3, 4, 4, 5}}.


I am not able to reach specific elements in a sublist. I can use a For loop for this, but it will slow down my program, so I want suggestions on a better Mathematica way of manipulating lists.

Edit

Sorry I guess my example was less than clear about what I am asking for. I want that any sublist in a[] that has all elements of any sublist from b[] shall be removed.

• Are you sure you want to write a[] and b[]? You aren't defining functions or using indexing, so simple identifiers such as a and b would be better in your case. Commented Jun 1, 2013 at 6:12
• ya actually they give same result. Commented Jun 1, 2013 at 6:41
• Not under all circumstances. Consider Clear[a]; a[] = 42; a = a[]; {a, a[]}. This gives {42, 42[]} as its output. Is that what you would expect or would want? Commented Jun 1, 2013 at 23:32
• thanks for correcting me precisely. Commented Jun 2, 2013 at 6:37

Mathematica supports the complement of a set:

Complement[{{1, 2}, {2, 3, 4, 4}, {3, 4, 4, 5}}, {{1, 2}, {4, 5, 6}}]
{{2, 3, 4, 4}, {3, 4, 4, 5}}


For the elaborated version, you can use Complement[] in tandem with DeleteCases[]:

DeleteCases[{{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}},
l_List /; Or @@ (Complement[#, l] === {} & /@ {{1, 2}, {4, 5, 6}})]
{{2, 3, 4, 4}, {3, 4, 4, 5}}

• I have amended my question..can you please look into it ? Commented May 31, 2013 at 18:18
• You should've been explicit to begin with... wait. Commented May 31, 2013 at 18:20
• Complement[#, l] === {} signifies what..can u someone explain how its working? Commented Jun 8, 2013 at 14:18
• In the interest of teaching you how to fish: start by comparing the result of Complement[#, {1, 2}] === {} & /@ {{1, 2}, {4, 5, 6}} and Complement[#, {2, 3, 4, 4}] === {} & /@ {{1, 2}, {4, 5, 6}} Commented Jun 8, 2013 at 15:07

In versions 10.2+, you can use ContainsAll combined with Pick, Select, Cases and DeleteCases:

Pick[a[], Nor @@ Through[(ContainsAll /@ b[])@#] & /@ a[]]
Select[a[], Nor @@ Through[(ContainsAll /@ b[])@#] &]
Cases[a[], _?(Nor @@ Through[(ContainsAll /@ b[])@#] &)]
DeleteCases[a[], _?(Or @@ Through[(ContainsAll /@ b[])@#] &)]


all give

{{2, 3, 4, 4}, {3, 4, 4, 5}}

a = {{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}};
b = {{1, 2}, {4, 5, 6}};

Pick[a, Nor @@@ Outer[SubsetQ, a, b, 1]]


{{2, 3, 4, 4}, {3, 4, 4, 5}}

a = {{1, 2}, {1, 2, 3}, {2, 3, 4, 4}, {3, 4, 4, 5}};

b = {{1, 2}, {4, 5, 6}};


Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[a, {x_} /; Or @@ Map[ContainsAll[x, #] &, b]]


{{2, 3, 4, 4}, {3, 4, 4, 5}}