# Is there a convention on which name to use for a throwaway variable? (Like underscore in Python) [duplicate]

I would prefer to write {_, foo} = LongFunctionName[arg1, arg2] instead of foo = LongFunctionName[arg1, arg2][[2]] or foo = Last @ LongFunctionName[arg1, arg2]. Using an actual name for the throwaway variable makes code unnecessarily verbose and can be misleading/confusing. Is it a good idea to use \$ for throwaways?

## marked as duplicate by Mr.Wizard♦Aug 8 '18 at 14:52

• I don't believe there is a convention. In fact I don't believe this is a common method at all, in Mathematica. This is closely related to (30138) which is probably the one @Kuba was thinking of. – Mr.Wizard Mar 9 '17 at 11:36
• +1, I would like to see a "throwaway variable" in Mathematica. MATLAB also has it, it uses the tilde. – C. E. Mar 9 '17 at 12:11
• @ZeitPolizei I would like to know if you would object to my closing this question as a duplicate of the one linked above. I feel that this is probably best addressed in a single place, and if there are aspects of your question that are not addressed there it may be better to extend that question rather than creating a separate one. If this action is taken your question will remain as an entry point for searches. – Mr.Wizard Mar 9 '17 at 23:28

Actually you can write {_, foo} = . . . and it works, as noted in How to ignore list elements when extracting with pattern matching.

If one is going to make use of this it may be desirable to turn Off Set::nosym.

Off[Set::nosym]

{a, _, b} = {1, 2, 3};

{a, b}

{1, 3}


Perhaps this is as close to a convention as we can find?

• Provided that LongFunctionName[...] immediately evaluates to a list of matching length, which it probably should for an undelayed assignment. – LLlAMnYP Mar 9 '17 at 13:15
• @LLlAMnYP Yes, that is an unstated assumption, since e.g. {a, b} := foo[x] doesn't work anyway. – Mr.Wizard Mar 9 '17 at 23:18
• @Mr.Wizard I suppose LLIAMnYP meant {a,b} = {1,2,3} case. – Kuba Mar 10 '17 at 7:51
• @Kuba well, that would be a similar problem. – LLlAMnYP Mar 10 '17 at 8:37
• @LLlAMnYP Ok, I just wasn't sure if there is a misunderstanding or not :) – Kuba Mar 10 '17 at 8:39

This one should not need to suppress messages:

Block[{a}, {a, b, a} = {1, 2, 3}];
a
b
(* a *)
(* 2 *)