7
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ Related: (7996) $\endgroup$ – Mr.Wizard Sep 13 '17 at 12:46
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Sep 13 '17 at 13:14
  • $\begingroup$ @ Mr.Wizard: unfortunately in my original data the sub-arrays can all have slightly different lengths. $\endgroup$ – mrz Sep 13 '17 at 14:26
  • 1
    $\begingroup$ ArrayFlatten[{{-1, #}}] & /@ list (see stackoverflow.com/a/2274679/499167) $\endgroup$ – user1066 Sep 13 '17 at 19:52
13
$\begingroup$

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
$\endgroup$
  • 3
    $\begingroup$ I quite like list[[All, All, 0]] = {-1, ##} &;. Already upvoted. $\endgroup$ – Mr.Wizard Sep 13 '17 at 13:19
  • $\begingroup$ Thank you for the different solutions ... $\endgroup$ – mrz Sep 13 '17 at 15:33
8
$\begingroup$
☺ = {-1, ##} & @@@ # & /@ # &; 

☺ @ list

(* thanks: Mr.Wizard *)

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

If we're golfing ;-)

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

Using Outer

Outer[Prepend[-1], list, 2]
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Sep 14 '17 at 16:40
6
$\begingroup$
Map[Prepend[#, -1] &, list, {2}]
$\endgroup$
6
$\begingroup$
Replace[list, a___ :> Catenate@{{-1}, a}, {2}]
$\endgroup$
6
$\begingroup$

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
$\endgroup$
6
$\begingroup$

Adding Query to the mix:

list // Query[All, All, Prepend[-1]]
$\endgroup$
2
$\begingroup$

A Flatten[] variation:

Flatten[{ConstantArray[{-1}, Length[#]] & /@ list, list}, {{2}, {3}, {1, 4}}]
$\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.