# Prepend a column of data to a given list

Suppose I have a list named DATA containing ten {x, y} elements. I need to prepend a column of data with 10 rows ranging from 0.1 to 1 with stepsize 0.1.

DATA = {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15}, {1,
2}, {55, 0}, {10, 1}}
Table[Nest[Prepend[#, i] &, DATA[[j]], 1], {i, 0.1, 1, 0.1}, {j, 1, 10}]


However, the result is something bizarre. It is coming out to be a three-dimensional matrix.

How do I do it? It is due to some minor problem I guess, but I am unable to point it out.

data= {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15}, {1,
2}, {55, 0}, {10, 1}}

Transpose@Join[{Range[0.1, 1, 0.1]}, Transpose[data]]


OR

MapThread[Join, {List /@ Range[0.1, 1, 0.1], data}]


result:

{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9,
5}, {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10,
1}}

• Thank you so much. You just helped me save weeks for calculation. Apr 7, 2022 at 11:27
• I am glad I was able to save you weeks. Thanks for the accept.
– Syed
Apr 7, 2022 at 11:27
• or MapThread[Join[{#1},#2]&, {Range[0.1, 1, 0.1], data}] , nearly the same. Apr 7, 2022 at 14:03

I noticed that you want 0.1 ~ 1 to be prepended. (index/10)

So this also works.

MapIndexed[Prepend[#1,First[#2]/10.]&,data]


{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5}, {0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}

Using the level argument of Join

Join[List /@ Range[0.1, 1, 0.1], data, 2]


gives the expected result:

{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5},
{0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}

data= {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5}, {7, 6}, {25, 15},
{1, 2}, {55, 0}, {10, 1}};

MapThread[Prepend,{data, Range[0.1, 1, 0.1]}]

(*{
{0.1,3,4},
{0.2,5,1},
{0.3,5,2},
{0.4,1,8},
{0.5,9,5},
{0.6,7,6},
{0.7,25,15},
{0.8,1,2},
{0.9,55,0},
{1.,10,1}
}


In more general terms:

mat = Array[a, {3, 4}];
vect= {10,20,30};


(1) Insert vect into mat as column 2:

MapThread[Insert[##,2]&, {mat, vect}]

(* {
{a[1,1],10,a[1,2],a[1,3],a[1,4]},
{a[2,1],20,a[2,2],a[2,3],a[2,4]},
{a[3,1],30,a[3,2],a[3,3],a[3,4]}
} *)


vect may be a column from mat:

MapThread[Insert[#,#[[3]],1]&,{mat}]

(*{
{a[1,3],a[1,1],a[1,2],a[1,3],a[1,4]},
{a[2,3],a[2,1],a[2,2],a[2,3],a[2,4]},
{a[3,3],a[3,1],a[3,2],a[3,3],a[3,4]}
}*)


(2) Append vect to mat as a column:

MapThread[Append, {mat, vect}]

(*{
{a[1,1],a[1,2],a[1,3],a[1,4],10},
{a[2,1],a[2,2],a[2,3],a[2,4],20},
{a[3,1],a[3,2],a[3,3],a[3,4],30}
}*)


(3) Prepend vect to mat as a column:

MapThread[Prepend, {mat, vect}]

(*{
{10,a[1,1],a[1,2],a[1,3],a[1,4]},
{20,a[2,1],a[2,2],a[2,3],a[2,4]},
{30,a[3,1],a[3,2],a[3,3],a[3,4]}
}*)


Using Do and PrependTo:

data = {{3, 4}, {5, 1}, {5, 2}, {1, 8}, {9, 5},
{7, 6}, {25, 15}, {1, 2}, {55, 0}, {10, 1}};

Module[{l = data}, Do[PrependTo[l[[i]], #[[i]]], {i, Length@#}] &@Range[0.1, 1, 0.1]; l]

(*{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5},
{0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}*)


Or using Cases:

Cases[Thread@{List /@ Range[0.1, 1, 0.1], data}, {a_, b_} :> Catenate@{a, b}]

(*{{0.1, 3, 4}, {0.2, 5, 1}, {0.3, 5, 2}, {0.4, 1, 8}, {0.5, 9, 5},
{0.6, 7, 6}, {0.7, 25, 15}, {0.8, 1, 2}, {0.9, 55, 0}, {1., 10, 1}}*)