# Using MapThread with pure function and variable number of elements

I have a variable size array of lists and would like to set up a MapThread of the lists with a variable pure function $$\{\#1,\#2,...,\#n\}\&$$ with $$n$$ being the number of rows in the array and am unable to code this as if I try constructs like

function = Table[
"#" <> ToString[n],
{n, 1, 5}]

(* {"#1", "#2", "#3", "#4", "#5"} *)


or:

function = Table[
ToString[#] <> ToString[n],
{n, 1, 5}]

(*  {"#11", "#12", "#13", "#14", "#15"} *)


and then MapThread the function like:

MapThread[
function &, {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2,3}}]


incompatible dimensions of objects at positions {2, 4} and {2, 5} of
dimensions are 3 and 2

Can someone help me with this or perhaps refer a link that would demonstrate this type of construct?

Thanks.

• Adding a bounty suggest that none of the answers so far were sufficient. But they all seem to solve the problem as presented. Is there more information you can provide to indicate why these answers don't suffice? Commented Feb 20 at 17:57
• They are all sufficient. I just want to reward an existing answer as per the bounty description. Also trivial looking, it was a bottleneck in my work and the solutions helped my progress and I appreciate the expertise given.
– josh
Commented Feb 20 at 18:09

I'm curious about why you think you need to specify all the slots. MapThread works just fine like this:

list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};

(* {g[1, 4, 1, 4, 1], g[2, 5, 2, 5, 2], g[3, 6, 3, 6, 3]} *)


And in your specific example, we can just swap g for List:

MapThread[List, list]
(* {{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}} *)


And if you do need slots for some reason, you don't need to specify all of them. You can just use SlotSequence:

MapThread[h[##] &, list]
(* {h[1, 4, 1, 4, 1], h[2, 5, 2, 5, 2], h[3, 6, 3, 6, 3]} *)

• Thanks for that. In an earlier comment which I deleted, I thought List wouldn't work but was only due to my lack of understanding. The construct MapThread[List,myList] is really concise and reduces the amount of code..
– josh
Commented Feb 19 at 14:24

No need to make list of each slot, use ## which is sequence of all slots/arguments.

MapThread[{##} &, {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2,
3}}]


{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}


The same can be achieved by:

Transpose@{{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}}

list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};

fun = Table[Slot @ n, {n, 5}];



{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}

The Evaluate is needed because Table has the attribute HoldAll

list = {{1, 2, 3}, {4, 5, 6}, {1, 2, 3}, {4, 5, 6}, {1, 2, 3}};


Another way using Thread and MapApply:

Thread[list]

(*{{1, 4, 1, 4, 1}, {2, 5, 2, 5, 2}, {3, 6, 3, 6, 3}}*)