# Creating a new list by dropping sub elements of the original list

Suppose I have a nested listed as follows,

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


Where the elements are,

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

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

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


I would I create a new list where I drop any sub element that has 1 or 2 in its first part. The output should then look like this,

{{{3,4,5}},{{5,4,1}},{{3,4,5},{3,4,5}}}


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

Select[#, FreeQ[#[[1]], 1 | 2] &] & /@ lst


or

 Pick[#, FreeQ[#[[1]], 1 | 2] & /@ #] & /@ lst


or

 Cases[#, _?(FreeQ[#[[1]], 1 | 2] &)] & /@ lst


or

 DeleteCases[#, _?(! FreeQ[#[[1]], 1 | 2] &)] & /@ lst


all give

{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

• @Ibrahim, you might want to wait a while before accepting an answer to encourage alternative answers. Welcome to mma.se.
– kglr
Jul 28, 2014 at 20:59
• @RunnyKine, thank you.. "almost" is critical :)
– kglr
Jul 28, 2014 at 21:11
list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}}


Then:

Cases[{Except[1 | 2], __}] /@ list  (* v10 syntax *)


{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

OR

Map[Cases[{Except[1 | 2], __}]]@list  (* v10 operator form *)


OR

Select[#, #[[1]] != 1 && #[[1]] != 2 &] & /@ list


OR

Select[#[[1]] != 1 && #[[1]] != 2 &] /@ list  (* v10 syntax *)

• Indeed, a subtle but significant change between V9 and V10. How did you discover it? Is it in the documentation?
– eldo
Jul 28, 2014 at 21:53
• @eldo. Yes it's in the documentation, I believe that's where I learned it. Jul 28, 2014 at 22:01

DeleteCases with levelspec:

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

DeleteCases[expr, {1 | 2, __}, {2}]

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}


Or, inspired by RunnyKine's answer, in v10 operator syntax:

DeleteCases[{1 | 2, __}] /@ expr

{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}

• +1 I tried using levelspec but kept botching it Jul 28, 2014 at 22:43
• @RunnyKine I like your use of the operator syntax, by the way. :-) Jul 28, 2014 at 22:43
• Thanks, I'm really liking the new operator forms. :) Jul 28, 2014 at 22:45
• @RunnyKine I just use Infinity because I'm lazy. Jul 28, 2014 at 23:05
• @seismatica That's fine, but be aware that it can come at a significant cost to performance. Most of the time not, but sometimes. Starting perhaps a year and a half ago I try to be more specific with my use of levelspec. Jul 29, 2014 at 0:39

If the list is all numbers, here's another way:

list = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}};
drop = {1, 2};

Pick[list,
Unitize[
Evaluate[Times @@ (# - drop)] & @
Map[
First,
list,
{2}]
],
1
]
(*
{{{3, 4, 5}}, {{5, 4, 1}}, {{3, 4, 5}, {3, 4, 5}}}
*)


Edit: changed the function applied to Map (thanks, Mr.Wizard), which originally was

Evaluate[ReleaseHold @ Fold[#1 (Hold[Slot][1] - #2) &, 1, drop]] &

• That's a twisted use of Hold[Slot], sure to confuse half the people who read this, and really quite unnecessary as well. Naturally, +1. :D Jul 29, 2014 at 0:47
• @Mr.Wizard I guess so, or I'm getting tired. BTW, I tried Inactive but it was slower than Hold, I think just by a constant (not depending on list). Not sure why. Maybe I'll fix the Hold... :) Jul 29, 2014 at 0:50
• Really I figured it was intentional obfuscation. Can't you just write Evaluate[Times @@ (# - drop)] &? Or maybe I'm tired. Jul 29, 2014 at 0:56
• @Mr.Wizard No, I think it's me who's tired. I started with something else that morphed while my brain wore out. Thanks for the simple code. Jul 29, 2014 at 0:59
• Ah, one of those sessions. Say no more. :o) Jul 29, 2014 at 0:59

Since all the good answers have been given, here's a dumb one:

expr = {{{1, 2}, {3, 4, 5}, {2, 3}}, {{2, 3, 4}, {5, 4, 1}, {1, 4, 3}}, {{3, 4, 5}, {3, 4, 5}}};
expr //. {pre___, {1 | 2, ___}, post___} :> {pre, post}