To synthesize what has been said in the comments to the question:
- Several options are available, some of them making use of relatively new features of Mathematica
- There isn't a strong consensus to decide if some solution is better style than the other
So in no particular order:
Map to Nothing
somelist /. 0->Nothing
Suggested by myself in the question
Works well, but Nothing being a relatively new addition to the language this solution won't appear in older code
In addition, Henrik Schumacher strongly discourage to get into the habit of using this solution large numerical data. The reason is "Nothing unpacks arrays." Henrik continues by saying, for large numerical data "usually the three-argument version of Pick is the fastest because that allows you to use a packed array as the second argument (unfortunately, Boolean arrays cannot be packed)"
Using Pick
Removing matching items is actually the same thing a picking only non matching items.
However, due to the way pattern matching is implemented, using Pick with Except can lead to surprising results. Here is a working solution:
Pick[somelist, somelist, Except[0, _Integer]]
Other possible uses of Pick to remove items are:
Pick[somelist, #!= 0 & /@ somelist ]
Pick[somelist,UnequalTo[0] /@ somelist ]
Pick[somelist,EqualTo[0] /@ somelist , False]
DeleteCases
DeleteCases[0]@somelist
DeleteCases[0|1|2]@somelist
Suggested by Michael E2
DeleteCases "is a common solution, as is its complementary function Cases. Select and Pick can be used, too, to filter a list"
Cases
Based on Michael's suggestions above, I would add:
Cases[somelist, Except[0]]
Select
Select[somelist, # != 0 &]
Select[somelist, UnequalTo[0]]
DeleteCases[0]@somelist? Extension:DeleteCases[-2 | 0 | 2]@somelist. – Michael E2 Dec 09 '19 at 01:21/.? – Sylvain Leroux Dec 09 '19 at 01:32DeleteCasesis a common solution, as is its complementary functionCases.SelectandPickcan be used, too, to filter a list.Nothingis relatively new to the language, so it won't appear in older code; however, it doesn't seem like a bad choice. Each approach has its virtues, and which to use might depend on the case at hand. For large packed arrays, this would be efficient for deleting0:Pick[somelist, Unitize@somelist, 1]. – Michael E2 Dec 09 '19 at 01:57Nothingunpacks arrays:Range[3] /. 1 -> Nothing // Developer`PackedArrayQ. Therefore I strongly discourage to get into the habit of using it for large numerical data. In that case, usually three-argument version ofPickis the fastest because that allows you to use a packed array as second argument (unfortunately, Boolean arrays cannot be packed). – Henrik Schumacher Dec 10 '19 at 11:57Pickyou've suggested, but with confusing results. This works:Pick[Range[5], Range[5], Except[0]]. But this doesn't work:somelist = Flatten[{Range[-5,5],Range[-3,3]}] ; Pick[somelist, somelist, Except[0]]– Sylvain Leroux Dec 10 '19 at 13:48Pickdoes not work with patterns. This is why it is faster. Try e.g.,Pick[Range[-5,5], Unitize[Range[-5,5]], 1]– Henrik Schumacher Dec 10 '19 at 13:55Pickdoc, I came to that solution that does work:Pick[somelist, somelist, Except[0, _Integer]]– Sylvain Leroux Dec 10 '19 at 14:00