# Thread Part ([[]]) over two lists

When I try to Thread Part function over two nested arrays, the operation is performed as expected but i get a warning. I'm not sure if (and what) am I doing wrong. The code example is below.

lst1 = {{5, -9, 15}, {12, -15, 4}}
indices={{2,1,3},{1,3,2}}


and the output is

{{-9, 5, 15}, {12, 4, -15}}


as expected. However, the following warning was thrown.

Part::pkspec1: The expression {{2,1,3},{1,3,2}} cannot be used as a part specification. >>


Shouldn't Part be threaded over the list as described in the help under Thread function?

• MapThread[Part, {lst1, indices}]...
– ciao
Jan 22 '17 at 0:25
• sometimes I use this kind of syntax : Thread[myPart[lst1,indices]] /. myPart -> Part. I don't know if it is a good practise. Jan 22 '17 at 0:30
• @andre, yeah, that also seams plausible solution. But RunnyKine offered a neat solution which tackle the core of the problem. Jan 22 '17 at 0:48
• Related: (4390) Jan 22 '17 at 7:09

RunnyKine already explained in detail the source of the message. I would like to offer a couple of alternative formulations of a solution and comment on your attempt to use Hold.

You commented:

I tried Thread[Hold[Part[lst1,indices]]]//ReleaseHold. Apparently placed Hold at wrong place.

Indeed, for two reasons.

1. In this expression Hold is the active head of the argument of Thread so, if it were possible, it is the head that would be distributed:

Thread[Hold[{1, 2, 3}]]

{Hold[1], Hold[2], Hold[3]}

2. For Thread to operate it must see explicit arguments at second level of the expression. Hold here prevents lst1 and indices from evaluating to their full forms therefore even if (1) is solved a problem remains. To illustrate this we can use Unevaluated which is like a temporary Hold that prevents evaluation but is transparent to the function in which it appears, i.e. Thread:

Thread[Unevaluated[Part[lst1, indices]]]  (* Part::pkspec1 printed *)


RunnyKine's use of Inactive and andre's use of Hold[Part] each get around both these problems because:

1. They do not introduce an additional level in the expression; Hold[Part] is a compound head.

TreeForm /@ {Hold[head[1, 2]], Hold[head][1, 2]}


2. While Part is rendered inactive the arguments of the compound head still evaluate in the default manner therefore lst1 and indices are expanded.

Another way to temporarily inactivate Part is Block:

Block[{Part}, Thread @ lst1[[indices]] ]


But as already demonstrated by ciao (rasher) this kind of inhibition is not needed as MapThread allows exactly the evaluation you want: expansion of lst1 and indices before Part is applied and evaluated.

• @Mr.Wizadr Suggestion: Perhaps you can consider expanding on this topic and adding one section to this post link about most common pitfalls. Grasping hold of these evaluation order was already difficult let alone when one includes pure functions and Hold constructs. Jan 22 '17 at 12:24
• @ercegovac Indeed, the evaluation sequence in Mathematica is quite a beast, but understanding it is essential for any of the more advanced things. Mr.W +1 quite the scholarly summary. Oct 30 '17 at 10:31

To understand what is going on here let's do the same evaluation with a symbol with no value and monitor it with Trace:

Remove[f];

(*

{{{lst1, {{5,-9,15}, {12,-15,4}}}, {indices, {{2,1,3}, {1,3,2}}},
{12,-15,4}}, {{2,1,3},{1,3,2}}]], {f[{5,-9,15}, {2,1,3}], f[{12,-15,4}, {1,3,2}]}}

*)


Here we see that indices was evaluated before being passed to f, so the error occurs before Thread gets its inputs. Now replace f with Part and you see the reason for the error. Part does not accept a list of lists as input only a 1D list is supported. To get around this, you can temporarily make Part inactive, do the evaluation then activate it as follows:

Thread[Inactive[Part][lst1, indices]] // Activate


{{-9, 5, 15}, {12, 4, -15}}

as expected with no error messages. There are other ways to achieve the same thing, we can use f as described and later replace f with Part.

• Thank you @RunnyKine. I suspected that to be problem but instead of Activate i was trying Hold which did not work. Though I am still novice in Mathematica... Jan 22 '17 at 0:44
• @ercegovac Thread[Hold[Part][lst1,indices]] //ReleaseHold works fine ! Jan 22 '17 at 0:52
• @andre Thanks! I tried Thread[Hold[Part[lst1,indices]]]//ReleaseHold. Apparently placed Hold at wrong place. Jan 22 '17 at 0:56