# How to ignore list elements when extracting with pattern matching

If I have a simple list, say {1, 2, 3}, and I want to extract a subset of the elements, what is the correct syntax? e.g.

{ i, _, j } = {1, 2, 3}


I want i = 1 & j = 3 (which works), but I get the following warning:

Set::nosym: _ does not contain a symbol to attach a rule to.


so clearly I am using the wrong placeholder for elements I wish to ignore.

-
The warning appears because you are trying to assign 2 to _. There are a lot of ways to do what you need: {i, j} = Drop[{1,2,3}, {2}] for example. – Kuba Aug 9 '13 at 10:15
It should be noted that often this type of situation is easily avoidable by instead doing something like: {i,j}={1,2,3}[[{1, 3}]]. – jVincent Aug 9 '13 at 13:01

You could always designate a Symbol for this purpose as a kind of dev/null, using e.g. $Post to clear it every time. I'll pick \[DoubleDagger], entered EscddgEsc and displayed as ‡: $Post = ((‡ =.; #) &);


Now you could make your assignment:

{i, ‡, j} = {1, 2, 3};


The value of ‡ is cleared after each evaluation so as not to take up memory.

For reference, the definition of $Post above is not entirely neutral. For example, by default entering Sequence @@ {1, 2, 3} will return Sequence[1, 2, 3] whereas with the definition it will return 1. The ugly but proper definition would be something like: $Post = Function[x, ‡ =.; Unevaluated@x, HoldAllComplete];


Since \$Post is only one way to clear the Symbol I didn't want to clutter the top of the answer with this code. Other methods would be RunScheduledTask, CellEpilog, etc.

-
I just got a new favorite dummy symbol! And very interesting answer too! – Pinguin Dirk Aug 9 '13 at 11:05
@Pinguin Thanks. I think [Esc] :-@ [Esc] is a nice option. :^) – Mr.Wizard Aug 9 '13 at 11:15
good thing they "announced" retina support for the Mac, that'll look great in the code – Pinguin Dirk Aug 9 '13 at 11:26

The equal sign is trying to assign the list {1,2,3} to the left hand side {i,_,j} and you cannot assign 2 to _.

Try the following code:

list = {a, b, c};
Extract[list, {{1}, {3}}]
{First[list], Last[list]}
Replace[list, {i_, _, j_} :> {i, j}]

-
Thanks. I was aware of the more verbose syntax for Extract which will certainly work, but I was hoping there was a more natural, concise syntax similar to that which I suggested? – fhusb Aug 9 '13 at 10:34