Skip to main content

list manipulation - How to "ignore" an element of Map or MapIndexed


Say I have some function that I'm applying every element in a list to... if that element matches some criteria:



If[#==,{#},  ] & /@ LongList

Is there a way to do something like this? I want the result to just not have that element.



Answer



This specific behaviour can be achieved using


If[condition, something, Unevaluated@Sequence[]]& /@ list

The key is Sequence[]. Unevaluated prevents it from disappearing from inside the If.


Alternatively you can use Cases (or many other solutions shown in other answers and comments---some of these solutions may be better suited for the problem but Sequence[] has its place too).


Cases[list, element_ /; condition :> something]

Comments