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

2 Answers 2


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.

  • $\begingroup$ 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. $\endgroup$
    – Student4K
    Nov 18, 2015 at 22:46
  • $\begingroup$ Yes, $z would work just fine. Welcome to Mathematica.StackExchange. $\endgroup$
    – bbgodfrey
    Nov 18, 2015 at 22:48
  • 1
    $\begingroup$ @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. $\endgroup$
    – bbgodfrey
    Nov 18, 2015 at 23:34
  • $\begingroup$ or f[x_] = {x^2, x^3, x^4, x^5}; Module[{z}, {a1, z, z, a2} = f[2]] $\endgroup$ Nov 19, 2015 at 0:18
  • $\begingroup$ @bbgodfrey I had messages window hiden so I haven't noticed it :P but anyway, what was I expecting? :P $\endgroup$
    – Kuba
    Nov 19, 2015 at 7:25

I would recommend to write

a1 = f[2][[1]]



{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]];




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.