10
$\begingroup$

It's been over a year since I've used Mathematica, and I'm having a total brain fart on a home project. I'd like to take an existing list of lists, say,

x = {{1, 2}, {3, 4}, {5, 6}}

and append 9 and 10 onto the end of each sublist of x to get a list y, where

y = {{1, 2, 9}, {1 ,2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}

I'm sure there's a built-in function to do this, but cannot recall it for the life of me.

$\endgroup$
1
  • 2
    $\begingroup$ Closely related: (13748) $\endgroup$
    – Mr.Wizard
    Jul 15, 2014 at 8:38

9 Answers 9

13
$\begingroup$
x = {{1, 2}, {3, 4}, {5, 6}};
y = {9, 10};
Flatten /@ Tuples[{x, y}]
(*
{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}
*)
$\endgroup$
1
  • $\begingroup$ Tuples. That was the function I had forgotten. $\endgroup$
    – John
    Jan 22, 2013 at 12:46
9
$\begingroup$
Flatten /@ Distribute[{x, {9, 10}}, List]

=> {1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}

$\endgroup$
7
$\begingroup$

Another possibility using Outer:

x = {{1, 2}, {3, 4}, {5, 6}};
y = {9, 10};
Outer[# ~Join~ {#2} &, x, y, 1]

or

Outer[Flatten @ {##} &, x, y, 1]

Flatten the above result at level 1, if you want an output like in belisarius' answer.

$\endgroup$
2
  • 1
    $\begingroup$ +1. You could have used Outer[Append, x, y, 1] instead. $\endgroup$ Jan 22, 2013 at 10:11
  • $\begingroup$ @Leonid It's a little trick I learned... people have an irrational dislike of Append ("eeek! don't use AppendTo to grow a list", etc.), but not Join, although they both do the same thing (and are equally "problematic"). So the less one uses it in an answer, the better the chances of an upvote =) "Irrational" was probably the wrong word, because it is completely justified, but what I really mean is that the dislike is attached to the word rather than to what it does (or how). Of course, a +1 comment from you increases the chances of trickle down upvotes much more, so it's a net win :D $\endgroup$
    – rm -rf
    Jan 23, 2013 at 20:38
7
$\begingroup$

Some I wrote before reading other answers, from shortest to longest:

x = {{1, 2}, {3, 4}, {5, 6}};

Append @@@ Tuples @ {x, {9, 10}}

Join @@ Outer[Append, x, {9, 10}, 1]

Flatten[{{##, 9}, {##, 10}} & @@@ x, 1]

Join @@ Thread /@ ArrayFlatten@{{x, {9, 10}}}

Join @@ Table[a ~Append~ b, {a, x}, {b, {9, 10}}]

And I just thought of:

## &[{##, 9}, {##, 10}] & @@@ x
$\endgroup$
5
$\begingroup$

Update: ... forgot Table!!

 Join @@ Table[Flatten@{i, j}, {i, x}, {j, y}]

or, with the new-in-Version-9 ArrayReshape,

 ArrayReshape[Table[{i, j}, {i, x}, {j, y}], {6, 3}]
 (* {{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}} *)

Join @@ (PadRight[x, {1 + Length@First@x, Length@x}, #] & /@ y) // Sort
(* {{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}} *)

or

Distribute[Append[{x}, y], List, List, List, Append]
Distribute[{x, y}, List, List, List, Append]
$\endgroup$
4
$\begingroup$

Just to be different:

MapIndexed[#1~Join~(10 - Mod[#2, 2]) &, Riffle[x, x]]
Join @@@ Riffle[Riffle[x, x], {{9}, {10}}, {2, -1, 2}]~Partition~2
(*{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}*)
$\endgroup$
4
$\begingroup$

Using Replace:

x = {{1, 2}, {3, 4}, {5, 6}}

Replace[x, {a_, b_} :> Sequence[{a, b, 9}, {a, b, 10}], {1}]

{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}

$\endgroup$
4
$\begingroup$

Using SequenceReplace:

Join @@ SequenceReplace[x, {x_} :> {Join[x, {9}], Join[x, {10}]}]

(*{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}*)

The following equivalent form is courtesy of @Syed:

SequenceReplace[x, {x_} :> Sequence[{Sequence @@ x, 9}, {Sequence @@ x, 10}]]

(*{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}*)
$\endgroup$
1
  • 1
    $\begingroup$ SequenceReplace[x, {x_} :> Sequence[{Sequence @@ x, 9}, {Sequence @@ x, 10}]] $\endgroup$
    – Syed
    Apr 14, 2023 at 18:13
1
$\begingroup$
flank[a_][b_] := Splice @ Map[Append[b, #] &, a]

flank[{9, 10}] /@ x

{{1, 2, 9}, {1, 2, 10}, {3, 4, 9}, {3, 4, 10}, {5, 6, 9}, {5, 6, 10}}

flank[{p, q}] /@ x

{{1, 2, p}, {1, 2, q}, {3, 4, p}, {3, 4, q}, {5, 6, p}, {5, 6, q}}

$\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.