7
$\begingroup$

In Python there is a a compact syntax for creating variables that refer to the various list elements.

>>> somelist = [’book.tex’, ’book.log’, ’book.pdf’]
>>> texfile, logfile, pdf = somelist
>>> texfile
’book.tex’
>>> logfile
’book.log’
>>> pdf
’book.pdf’

Of course the number of variables on the left-hand side must match the number of elements in the list. The only relevant I can think in Mathematica is MapThread (or Thread).

In[4]:= somelist = {"book.tex", "book.log", "book.pdf"};
MapThread[Set, {{texfile, logfile, pdf}, somelist}]

Out[5]= {"book.tex", "book.log", "book.pdf"}

Is there anything else similar in Mathematica? Thank you very much.

$\endgroup$
14
$\begingroup$

Yeah, you can do destructuring assignments:

someList = {"book.tex", "book.log", "book.pdf"};

{texFile, logFile, pdf} = someList;

texFile
(* "book.tex" *)

pdf 
(* "book.pdf *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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