7

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
mrz
  • 11,686
  • 2
  • 25
  • 81
  • 2
  • 1
    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
  • 1
    ArrayFlatten[{{-1, #}}] & /@ list (see https://stackoverflow.com/a/2274679/499167) – user1066 Sep 13 '17 at 19:52

9 Answers9

13

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
kglr
  • 394,356
  • 18
  • 477
  • 896
rhermans
  • 36,518
  • 4
  • 57
  • 149
8
☺ = {-1, ##} & @@@ # & /@ # &; 

☺ @ list

(* thanks: Mr.Wizard *)

kglr
  • 394,356
  • 18
  • 477
  • 896
7

If we're golfing ;-)

Prepend[-1]/@#&/@list
7

Using Outer

Outer[Prepend[-1], list, 2]
Anjan Kumar
  • 4,979
  • 1
  • 15
  • 28
  • 1
    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
6
Map[Prepend[#, -1] &, list, {2}]
eldo
  • 67,911
  • 5
  • 60
  • 168
6

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
aardvark2012
  • 5,424
  • 1
  • 11
  • 22
6
Replace[list, a___ :> Catenate@{{-1}, a}, {2}]
jiaoeyushushu
  • 911
  • 6
  • 10
6

Adding Query to the mix:

list // Query[All, All, Prepend[-1]]
WReach
  • 68,832
  • 4
  • 164
  • 269
2

A Flatten[] variation:

Flatten[{ConstantArray[{-1}, Length[#]] & /@ list, list}, {{2}, {3}, {1, 4}}]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574