# Operations on expressions within Hold [duplicate]

This question already has an answer here:

I want to convert {Hold[1 + 2], Hold[3 + 4], Hold[5*6]} into Hold[{1 + 2, 3 + 4, 5*6}].

I have tried

{Hold[1 + 2], Hold[3 + 4],  Hold[5*6]} /. {Hold[a_], Hold[b_], Hold[c_]} -> Hold[{a, b, c}]

Hold[{1 + 2, 3 + 4, 5 6}]


Is there a simpler way?

-

## marked as duplicate by Kuba, halirutan, Sjoerd C. de Vries, Artes, m_goldbergOct 26 '13 at 13:07

Related (perhaps duplicate): Join held lists, Injecting Sequence into Hold. – István Zachar Oct 26 '13 at 7:07

Join works with every Head so it seems to be the best way here:

Join @@ {Hold[1 + 2], Hold[3 + 4], Hold[5*6]}

Hold[1 + 2, 3 + 4, 5 6]


and if you really need those {} inside:

% /. Hold[x__] :> Hold[{x}]

Hold[{1 + 2, 3 + 4, 5 6}]

-

One way which works no matter how many elements your list has is to use Flatten

expr = {Hold[1 + 2], Hold[3 + 4], Hold[5*6]};
Flatten[Hold @@ expr, 1, Hold]

(* Hold[1 + 2, 3 + 4, 5 6] *)


You see that the list braces are not created. They could be reconstructed too but the question is whether you really need them. I assume for your purpose it is not really a difference whether you have List[e1,e2,...] or Hold[e1,e2,...] but Hold has the advantage of not evaluating your expression.

If you want the list, you can add another rule

expr /. {a__Hold} :> Flatten[Hold[a], 1, Hold] /.
Hold[a__] :> Hold[{a}]

(* Hold[{1 + 2, 3 + 4, 5 6}] *)


Or you define a function which basically does the same but the rules are hidden in the DownValues

f[{a__}] := f[a];
f[a__Hold] := f[Flatten[Hold[a], 1, Hold]];
f[Hold[a__]] := Hold[{a}];

f[expr]

(* Hold[{1 + 2, 3 + 4, 5 6}] *)

-

Here is an interesting way:

expr = {Hold[1 + 2], Hold[3 + 4], Hold[5*6]};
expr /. {Hold -> Sequence, List -> Hold}


Hold[1 + 2, 3 + 4, 5 6]

And if you want the inner List back:

expr /. {Hold -> Sequence, List -> Hold} /. Hold[x__] :> Hold[{x}]

Hold[{1 + 2, 3 + 4, 5 6}]

-