Prepending a constant value to sublists of a list

I have a list:

list =
{
{{1,1,{1,1}},{1,2,{2,2}},{1,3,{3,3}},{1,4,{4,4}},{1,5,{5,5}}},
{{2,1,{1.01,1.01}},{2,2,{2.01,2.01}},{2,3,{3.01,3.01}},{2,4,{4.01,4.01}},{2,5,{5.01,5.01}}},
{{3,1,{1.02,1.02}},{3,2,{2.02,2.02}},{3,3,{4.02,4.02}},{3,4,{5.02,5.02}}},
{{4,1,{1.52,1.52}},{4,2,{2.03,2.03}},{4,3,{3.52,3.52}},{4,4,{4.03,4.03}},{4,5,{5.03,5.03}}},
{{5,1,{1.53,1.52}},{5,2,{2.53,2.53}},{5,3,{3.53,3.53}},{5,4,{4.53,4.53}},{5,5,{5.054,5.54}}},
{{6,1,{1.54,1.54}},{6,2,{3.53,3.53}},{6,3,{3.54,3.54}},{6,4,{4.54,4.54}},{6,5,{6.054,6.54}}}
};

Now I want to prepend to each sublist item a -1, so that I get:

{
{{-1,1,1,{1,1}},{-1,1,2,{2,2}},{-1,1,3,{3,3}},{-1,1,4,{4,4}},{-1,1,5,{5,5}}},
{{-1,2,1,{1.01,1.01}},{-1,2,2,{2.01,2.01}},{-1,2,3,{3.01,3.01}},{-1,2,4,{4.01,4.01}},{-1,2,5,{5.01,5.01}}},
{{-1,3,1,{1.02,1.02}},{-1,3,2,{2.02,2.02}},{-1,3,3,{4.02,4.02}},{-1,3,4,{5.02,5.02}}},
{{-1,4,1,{1.52,1.52}},{-1,4,2,{2.03,2.03}},{-1,4,3,{3.52,3.52}},{-1,4,4,{4.03,4.03}},{-1,4,5,{5.03,5.03}}},
{{-1,5,1,{1.53,1.52}},{-1,5,2,{2.53,2.53}},{-1,5,3,{3.53,3.53}},{-1,5,4,{4.53,4.53}},{-1,5,5,{5.054,5.54}}},
{{-1,6,1,{1.54,1.54}},{-1,6,2,{3.53,3.53}},{-1,6,3,{3.54,3.54}},{-1,6,4,{4.54,4.54}},{-1,6,5,{6.054,6.54}}}
}

To do that I could use:

Prepend[#, -1] & /@ list[[#]] & /@ Range[Length[list]]

Do you know another solution for that?

• Related: (7996) – Mr.Wizard Sep 13 '17 at 12:46
• FWIW if your third sub-array were not shorter than the others you could use ArrayPad[list, {0, 0, {1, 0}}, -1] -- this may still prove useful in other applications. In this case we would need ArrayPad[#, {0, {1, 0}}, -1] & /@ list which seems clumsy compared to other methods. – Mr.Wizard Sep 13 '17 at 13:14
• @ Mr.Wizard: unfortunately in my original data the sub-arrays can all have slightly different lengths. – mrz Sep 13 '17 at 14:26
• ArrayFlatten[{{-1, #}}] & /@ list (see stackoverflow.com/a/2274679/499167) – user1066 Sep 13 '17 at 19:52

Rewriting other's using operator forms

I like @eldo's solution, but it can be shorter!

Map[Prepend[-1], list, {2}]

Same for @aardvark2012's

Map[Insert[-1, 1], list, {2}]

Solutions of my own

list[[All, All, 0]] = Prepend[-1]@*List;

Or better

list[[All, All, 0]] = {-1, ##} &;

Also as an operator

ReplacePart[{_, _, 0} -> ({-1, ##} &)]

Best other

Given that the OP accepted mine (hastily to my taste and despite the good quality of the others), I want to highlight this answer given by Mr.Wizard in a comment to another answer. Easy to understand, and short:

Map@Prepend[-1] /@ list
• I quite like list[[All, All, 0]] = {-1, ##} &;. Already upvoted. – Mr.Wizard Sep 13 '17 at 13:19
• Thank you for the different solutions ... – mrz Sep 13 '17 at 15:33
☺ = {-1, ##} & @@@ # & /@ # &;

☺ @ list

(* thanks: Mr.Wizard *)

• Shorter: {-1, ##} & @@@ # & /@ # & – Mr.Wizard Sep 13 '17 at 12:55
• @Mr.Wizard, yes! – kglr Sep 13 '17 at 12:56
• @Mr.Wizard {-1, ##}&@@@#&/@list is really very very nice! (And @kglr, of course I like the smilies and the typographic minimalism in v2 :-) ) – John Joseph M. Carrasco Sep 13 '17 at 20:50
• @John I have long enjoyed ## and @@@ and I like the newer operator forms of v10, so I find ☺ = Map[{-1, ##} & @@@ # &] a pleasing combination. – Mr.Wizard Sep 13 '17 at 21:18

If we're golfing ;-)

Prepend[-1]/@#&/@list
• Oh, nicely played! – rhermans Sep 13 '17 at 12:34
• Also Map@Prepend[-1] /@ list – Mr.Wizard Sep 13 '17 at 12:48
• @Mr.Wizard: I like this solution in the comment very much ... – mrz Sep 13 '17 at 15:30
• @mrz Thank you. – Mr.Wizard Sep 13 '17 at 21:14

Using Outer

Outer[Prepend[-1], list, 2]
• I don't know if I've ever seen Outer used this way. Even if it's just my memory failing me again thanks for showing me something unexpected. – Mr.Wizard Sep 14 '17 at 16:40
Map[Prepend[#, -1] &, list, {2}]
Replace[list, a___ :> Catenate@{{-1}, a}, {2}]

One possibility:

Map[Join[{-1}, #] &, list, {2}]

Edit 1: or

Map[Insert[#, -1, 1] &, list, {2}]

(possibly faster on large lists).

Edit 2: or

ArrayPad[#, {{0, 0}, {1, 0}}, -1] & /@ list