How to transform a list as
{{x,y},{a,b,c,d,e}}
into the form of
{{x,y,a},{x,y,b},{x,y,c},{x,y,d},{x,y,e}}
elegantly, without using a loop such as For
?
Flatten /@ Thread[data, List, {2}]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
Also:
Append @@@ Tuples[{{#}, #2}] & @@ data
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
ClearAll[dat]
dat = {{x, y}, {a, b, c, d, e}}
Append[dat[[1]], #] & /@ dat[[2]]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
Also this may offer some added advantages
Table[Join[dat[[1]], {dat[[2, i]]}], {i, Length[dat[[2]]]}]
Seems like a good place to use MapThread
. Problem is that it wants both lists to be the same length. So use Table
to make it so.
{p, q} = {{x, y}, {a, b, c, d, e}}; MapThread[Append, {Table[p,Length[q]], q}]
Here is a very simple way to do it.
data = {{x, y}, {a, b, c, d, e}};
{data[[1, 1]], data[[1, 2]], #}& /@ data[[2]]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
It's usually nice to have at least one method that preserves packed arrays, when the two lists in the input are packed arrays.
PadLeft[ArrayReshape[#2, {Length@#2, 1}], {Length@#2, 1 + Length@#1},
Reverse@#1] & @@ {{x, y}, {a, b, c, d, e}}
(* {{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}} *)
a1 = RandomInteger[9, 200];
a2 = RandomInteger[{100, 110}, 50000];
PadLeft[ArrayReshape[#2, {Length@#2, 1}], {Length@#2, 1 + Length@#1},
Reverse@#1] & @@ {a1, a2} // Developer`PackedArrayQ
(* True *)
For fun, a couple of obscure ones, which make nice puzzles:
Through[(Append /@ #2)[#1]] & @@ {{x, y}, {a, b, c, d, e}}
(* {{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}} *)
Flatten@Level[Map @@ {{x, y}, {a, b, c, d, e}}, {2}, Heads -> True] ~Partition~ 3
(* {{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}} *)
With {p, q} = {{x, y}, {a, b, c, d, e}};
Table[Append[p, q[[i]]], {i, 1, Length@q}]
or
Outer[Join, {p}, Partition[q, 1], 1, 1][[1]]
or
Join[p, {#}] & /@ q
all give
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
{p, q} = {{x, y}, {a, b, c, d, e}};
Using SequenceReplace
(new in 11.3)
SequenceReplace[q, {s_} :> Append[p, s]]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
Using SubsetReplace
(new in 12.1)
SubsetReplace[q, {s_} :> Append[p, s]]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
Using ReplacePart
and Splice
(new in 12.1)
ReplacePart[q, i_ :> {Splice @ p, q[[i]]}]
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
data = {{x, y}, {a, b, c, d, e}};
Using Thread
and ConstantArray
:
Append @@@ Thread[{ConstantArray[#1, Length@#2], #2} & @@ data]
(*{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}*)
list = {{x, y}, {a, b, c, d, e}}
Thread[Append[Sequence @@ list]]
Distribute[list, Apply[Thread@*Append]]
Result:
{{x, y, a}, {x, y, b}, {x, y, c}, {x, y, d}, {x, y, e}}
{p, q} = {{x, y}, {a, b, c, d, e}}; Append[p, #] & /@ q
$\endgroup$Outer[Join, {p}, Partition[q, 1], 1, 1][[1]]
$\endgroup$First[Outer[Flatten@*List, {#1}, #2, 1, 2] & @@ {{x, y}, {a, b, c, d, e}}]
$\endgroup$Join[p, {#}] & /@ q
$\endgroup$Join[]
was going to be quicker, turns out I was wrong :/. Your first comment is the most efficient of all of these. $\endgroup$