11
$\begingroup$

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?

$\endgroup$
7
  • 6
    $\begingroup$ {p, q} = {{x, y}, {a, b, c, d, e}}; Append[p, #] & /@ q $\endgroup$
    – ubpdqn
    Commented Nov 13, 2016 at 11:06
  • $\begingroup$ Outer[Join, {p}, Partition[q, 1], 1, 1][[1]] $\endgroup$
    – corey979
    Commented Nov 13, 2016 at 11:11
  • $\begingroup$ First[Outer[Flatten@*List, {#1}, #2, 1, 2] & @@ {{x, y}, {a, b, c, d, e}}] $\endgroup$ Commented Nov 13, 2016 at 11:11
  • 4
    $\begingroup$ Join[p, {#}] & /@ q $\endgroup$
    – Feyre
    Commented Nov 13, 2016 at 11:14
  • 1
    $\begingroup$ @ubpdqn I was sure Join[] was going to be quicker, turns out I was wrong :/. Your first comment is the most efficient of all of these. $\endgroup$
    – Feyre
    Commented Nov 13, 2016 at 11:19

9 Answers 9

7
$\begingroup$
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}}

$\endgroup$
1
5
$\begingroup$
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]]]}]
$\endgroup$
2
$\begingroup$

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}]
$\endgroup$
0
2
$\begingroup$

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}}

$\endgroup$
2
$\begingroup$

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}}  *)
$\endgroup$
1
$\begingroup$

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}}

$\endgroup$
1
$\begingroup$
{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}}

$\endgroup$
1
$\begingroup$
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}}*)
$\endgroup$
0
$\begingroup$
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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.