prepend multiple list with a same header

Use Prepend instead of PrependTo.

Prepend[#, header] & /@ {data1, data2, data3, data4}

(* { {{A,B},{1,1},{1,1}},
     {{A,B},{2,2},{2,2}},
     {{A,B},{3,3},{3,3}},
     {{A,B},{4,4},{4,4}} } *)

The difference is that PrependTo not only creates a new list with a prepended element but it also assigns the result back to the variable in the first argument:

myList = {1, 2, 3};

PrependTo[myList, "abc"]
(* {"abc", 1, 2, 3} *)

myList
(* {"abc", 1, 2, 3} *)

Contrast this to Prepend, which does not update the variable:

myList = {1, 2, 3};

Prepend[myList, "abc"]
(* {"abc", 1, 2, 3} *)

myList
(* {1, 2, 3} *)

The first argument to PrependTo must be an assignable variable:

PrependTo[{1, 2, 3}, "abc"]
(* PrependTo::rvalue:
     {1,2,3} is not a variable with a value, so its value cannot be changed. *)

... but Prepend makes no such demand:

Prepend[{1, 2, 3}, "abc"]
(* {"abc", 1, 2, 3} *)

For this reason, Prepend is more suitable for use with higher-level functions such as Map.

Responding to the Updated Question

If we wish to update the original variables, we need to prevent the evaluation of the individual variables. For example:

Scan[Function[Null, PrependTo[#, {"A", "B"}], HoldFirst], Hold[data1, data2, data3, data4]]

data1
(* {{"A","B"},{1,1},{1,1}} *)

data2
(* {{'A","B"},{2,2},{2,2}} *)

data3
(* {{"A","B'},{3,3},{3,3}} *)

data4
(* {{"A","B"},{4,4},{4,4}} *)

Note the use of Hold to prevent the variable list from being evaluated and the use of the HoldFirst attribute on the pure function for the same purpose.

A Plague Of (and On) Held Expressions

All of this holding seems very complicated for such a simple operation. This is unfortunately a common occurrence when working with held expressions. Since the first argument to PrependTo must be held, our mapped operator must hold that argument as well. Which, in turn, means that the list over which we wish to iterate must be held as well. And so on up the chain.

Held expressions are contagious, and once we want to start iterating over them they spread like the plague. In this simple example, there were two prospects for error: if we had omitted the Hold or HoldFirst. But in more complex programs the chance for so-called "evaluation leaks" to spring up grows quickly.

All of the built-in destructive operators (like PrependTo, Set, SetDelayed etc) require held arguments. In other languages, programs involving destructive operators are harder to reason about on account of their side-effects (I found the bug but... the original values are now overwritten and lost. whoops). But in Mathematica, destructive operators have the additional complication of potential evaluation leaks and hold-contagion.

This is why the Mathematica documentation stresses the transformative and functional programming styles -- precisely to avoid such complexity.

In the case at hand, the desired list can be created directly in a functional style:

Thread[{"data1","data2","data3","data4"} -> Prepend[header] /@ {data1,data2,data3,data4}]

(* { "data1" -> {{"A", "B"}, {1, 1}, {1, 1}}
   , "data2" -> {{"A", "B"}, {2, 2}, {2, 2}}
   , "data3" -> {{"A", "B"}, {3, 3}, {3, 3}}
   , "data4" -> {{"A", "B"}, {4, 4}, {4, 4}}
   } *)

There is no need to overwrite the original variables. (in earlier versions of Mathematica the operator mapped may need to be written as Prepend[#, header]&)


Map does not work with PrependTo because Map does not hold its variables and PrependTo requires a variable so that it can prepend to the value of that variable.

That is, Map does not pass data1 to PrependTo. Instead it resolves data1 to its value, {{1,1},{1,1}}, and then passes this value to PrependTo. However, PrependTo needs a variable (not the value of a variable).

You can get around this by using Prepend as @WReach has suggested.

{data1, data2, data3, data4} = Prepend[#, header] & /@ {data1, data2, data3, data4};

However, if your data* values are sufficiently large that you do not want to do the copy-prepend-assign then there is a solution with GeneralUtilities`HoldMap.

GeneralUtilities`HoldMap[PrependTo[#, header] &, {data1, data2, data3, data4}];

Then

data1
{{"A", "B"}, {1, 1}, {1, 1}}

and similarly for the other variables.

Hope this helps.


Unevaluated

You can Map Unevaluated over the held Symbols to keep PrependTo[#, header] & from evaluating each of them too soon.

Unevaluated /@ Hold[data1, data2, data3, data4] // Scan[# ~PrependTo~ header &]

data3
{{"A", "B"}, {3, 3}, {3, 3}}

Automated solutions

A slight modification to a method from WReach gives us a very clean solution:

SetAttributes[vars, HoldAll]
vars /: s : _[_vars, _] := CompoundExpression @@ Thread[Unevaluated @ s, vars, 1];

Now just write:

PrependTo[vars[data1, data2, data3, data4], header];

We could also apply my autoThread function, still under development:

PrependTo[{data1, data2, data3, data4}, header] // autoThread;

Alternative approach

If you wish to avoid all of this consider using indexed objects to store you data:

data[1] = {{1, 1}, {1, 1}};
data[2] = {{2, 2}, {2, 2}};
data[3] = {{3, 3}, {3, 3}};
data[4] = {{4, 4}, {4, 4}};

header = {"A", "B"};

This allows you to use:

PrependTo[data[#], header] & /@ {1, 2, 3, 4};

Or:

Array[PrependTo[data[#], header] &, 4];

Or:

Do[PrependTo[data[i], header], {i, 4}]

Recommended reading:

  • Assigning values to a list of variable names
  • Manipulating unevaluated expressions in nested function calls