8
$\begingroup$

I have two lists:

list1 = {{-21, -1}, {-19, -9}, {-19, 7}};
list2 = {{-4, -1}};

I would like to have this list

list = {{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19,7}}}

I tried

Union[list1, list2]

and got,

{{-21, -1}, {-19, -9}, {-19, 7}, {-4, -1}}

How can I get it to show this?

 list = {{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19,7}}}
$\endgroup$

8 Answers 8

9
$\begingroup$
Tuples[{list2, list1}]
(* {{{-4,-1},{-21,-1}},{{-4,-1},{-19,-9}},{{-4,-1},{-19,7}}} *)
$\endgroup$
10
$\begingroup$
Outer[List, list2, list1, 1]
(* {{{-4,-1},{-21,-1}},{{-4,-1},{-19,-9}},{{-4,-1},{-19,7}}} *)
$\endgroup$
9
$\begingroup$
list1 = {{-21, -1}, {-19, -9}, {-19, 7}};
list2 = {{-4, -1}};

Catenate[{list2, {#}}] & /@ list1

Join[list2, {#}] & /@ list1

Result:

{{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19, 7}}}

$\endgroup$
8
$\begingroup$

anther option

Given

lis1 = {{-21, -1}, {-19, -9}, {-19, 7}};
lis2 = {{-4, -1}};

Then

Map[{First@lis2, #} &, lis1]

Mathematica graphics

And for fun:

Join[Riffle[lis1, lis2], lis2]
RotateLeft[#, 1] & /@ Partition[%, 2]

Mathematica graphics

$\endgroup$
7
$\begingroup$
l1 = {{-21, -1}, {-19, -9}, {-19, 7}};

l2 = {{-4, -1}};

l3 = {{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19, 7}}};

Using Thread:

Activate[Reverse /@ Thread[{l1, Inactive[l2[[1]]]}]] === l3

(*True*)

Or using Replace at level 1:

Replace[l1, {a_, b_} :> l2~Append~{a, b}, {1}] === l3

(*True*)
$\endgroup$
6
$\begingroup$

Two additional methods:

Distribute[{list2, list1}, List] == list
True
Thread[{list2[[1]], list1}, List, {2}] == list
True
$\endgroup$
5
$\begingroup$
a = {{-21, -1}, {-19, -9}, {-19, 7}};

b = {{-4, -1}};

SequenceCases[Riffle[a, b, {2, -1, 2}], {a_, b_} :> {b, a}]

{{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19, 7}}}

$\endgroup$
3
$\begingroup$
Distribute[{list2, list1}, List]

(* {{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19, 7}}} *)

or

MapApply[{list2[[1]], {##}} &, list1]

(* {{{-4, -1}, {-21, -1}}, {{-4, -1}, {-19, -9}}, {{-4, -1}, {-19, 7}}} *)
$\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.