How to "ignore" an element of Map or MapIndexed
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]
In a similar vein to Szabolcs' first solution, you can use SlotSequence
to achieve the same effect.
If[condition, <do something>, ## &[]] & /@ list
You can combine Map
(aka /@
) and Select
:
In[242]:= Sqrt /@ Select[{1, -2, 3, -4, 5}, # > 0 &]
Out[242]= {1, Sqrt[3], Sqrt[5]}