Message Boards Message Boards


Can DeleteCases accept an anonymous function as a pattern?

Posted 3 months ago
4 Replies
2 Total Likes

I suspect this is just a misunderstanding on my part, but I want to check if what I am trying is not possible.

I have a list of lists. I want to delete elements that have a length of less than 20 items. I can get the following to work.

Select[meetingSet, Length[#] >= 20 &]

But I was surprised that

DeleteCases[meetingSet, Length[#] <20 &]  

Just returns the original list.

Is it possible to make the pattern an anonymous function?

Cheers, Andy

4 Replies
Posted 3 months ago


I'm not at a place where I can try anything right now. But, let me give this my best guess.

I think the answer might be yes, but the function would have to return a pattern. Your example returns true/false.


You need a Pattern in DeleteCases while Select uses a function that returns true/false. An example would be

DeleteCases[list, (x_ /; Length[x] < 20)]



You can use a pure function in DeleteCases if it is packaged as a PatternTest:

DeleteCases[{{1, 2}, Range[21]},
 _?(Length[#] < 20 &)]
Posted 3 months ago

Thank you very much everyone. I need to go and practice more with pattern matching. I can see it's very powerful, but I haven't properly understood it up to now.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract