# Ignore some values at return

Is there Mathematica analogue of Python _ (underscore) or Matlab ~, a special variable, that can be used when a function returns a list and only some of the list elements are needed while others just ignored. E.g.:

f[x_] = {x^ 2, x^ 3};
{a1, _} = f[2]

• In this particular case, you could use a1 = f[2][[1]]. Commented Nov 18, 2015 at 22:20
• Have a look at Part or Extract. Part would be shorter code. Both can select multiple items from the returned list. Commented Nov 18, 2015 at 23:01
• Is this ok: a /: Set[a, _] := Null? No value can be set to a, but can be SetDelayed etc.
– Kuba
Commented Nov 19, 2015 at 7:28

Instead of an underscore, a "throwaway" symbol can be used. For instance,

f[x_] = {x^2, x^3, x^4, x^5};
{a1, z, z, a2} = f[2]


a1 and a2 have their desired values, and the others are overwritten into z at the cost of only a single symbol.

• That seems to be the easiest way. I figured out the dollar sign is a legal variable name and probably might as well be used for trash-variable. Commented Nov 18, 2015 at 22:46
• Yes, \$z would work just fine. Welcome to Mathematica.StackExchange. Commented Nov 18, 2015 at 22:48
• @Kuba, Using a Formal Symbol is a clever idea (+1), although Off[Set::wrsym] would be necessary to avoid what could be many error messages. Commented Nov 18, 2015 at 23:34
• or f[x_] = {x^2, x^3, x^4, x^5}; Module[{z}, {a1, z, z, a2} = f[2]] Commented Nov 19, 2015 at 0:18
• @bbgodfrey I had messages window hiden so I haven't noticed it :P but anyway, what was I expecting? :P
– Kuba
Commented Nov 19, 2015 at 7:25

I would recommend to write

a1 = f[2][[1]]


4

Clear[a1]

{a1, _} = f[2];


also assigns 4 to a1 but produces an error message which you can turn off temporarily with Quiet:

Quiet[{a1, _} = f[2]];

a1


4