# How can I manipulate a simple list of vectors?

I'd like to remove the first element from each vector and adding it to the beginning of each subsequent element within the vector so that:

{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}

Becomes:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

I'd like to use >select< with the condition that the length is greater than 1 to get rid of the first elements after I have flattened the vector lists to the first level. However, how would I alter the vectors using the join command?

rogerRoger = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4,
15}, {9, 21}}};

Select[Flatten[Join[], 1], Length@# > 1 &]

• Flatten[lst /. {{x_}, {a_, b_}, {c_, d_}, {e_, f_}} -> {{x, a, b}, {x, c, d}, {x, e, f}}, 1] Commented Mar 21 at 4:02

{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}

Becomes:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

Can you not use If ?

lis ={{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9,21}}};
If[Length[#[[1]]] == 1, z = #[[1]]; Join[z, #] & /@ Rest[#], #] & /@ lis
Flatten[%, 1]


with the condition that length is greater than 1

I am not sure I understand why the above? Length of what?

You want to join the first list to the rest if the first list had length 1, right? That is what you example does.

So if first rest had length say 2, (greater than 1) it will not touch that list and will leave it as is. This is what I understood you wanted.

lis={{{1,3},{2,5},{5,10},{10,23}},{{2},{3,35},{4,15},{9,21}}};
If[Length[#[[1]]]==1,z=#[[1]];Join[z,#]&/@Rest[#],#]&/@lis
Flatten[%,1]


• Thanks that was really useful, as for the condition length greater than one it's the criteria and everything within flatten is the list for >select[list, criteria]< which is what I wanted yes.
– user85376
Commented Mar 3, 2022 at 2:18

Cases can be used.

list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};

Flatten[Cases[list, {x_, y__} :> (Join[x, #] & /@ {y})], 1]

(* {1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}} *)

lst = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};

Join @@ Map[Flatten /@ Thread[{#[[1, 1]], Rest @ #}] &] @ lst

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}


Also

Join @@ (Prepend[#[[1]]] /@ {##2} & @@@ lst)

Join @@ (Prepend @@@ Tuples[{{##2}, #}] & @@@ lst)

Flatten[Join @@@ Tuples[{{#}, {##2}}] & @@@ lst, 1]

Distribute[{{#}, {##2}}, List, List, Sequence, Join] & @@@ lst


all give

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}


And ... for fun:

☺☺☺ = ## & @@ (♯ |-> ## & @@@ {#, ♯}) /@ {##2} & @@@ # &;

☺☺☺ @ lst

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9,    21}}

list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};


Define a helper function,

join[a_List, b_List] := Join[a, #] & /@ b


Then,

list2 = Flatten[join[#[[1]], Rest@#] & /@ list, 1]

(* {1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}} *)

list =
{{{1}, {2,  5}, {5, 10}, {10, 23}},
{{2}, {3, 35}, {4, 15}, { 9, 21}}};


Using Function and Splice (new in 12.1)

(a |-> Splice @ Map[Prepend @ a[[1, 1]], Rest @ a]) /@ list


{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

Using Cases

Cases[list, {{a_}, b__} :> Splice[Prepend[a] /@ {b}], -1]


(* same result *)

Using SequenceReplace (new in 11.3)

SequenceReplace[{{a_}, b : Except[{_}] ..} :>
Splice[Prepend[a] /@ {b}]][Join @@ list]


(* same result *)

list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9, 21}}};


Using Replace at level 1:

Replace[list, l_ :> Sequence @@ Join @@@ Tuples[{{l[[1]]}, {##2} & @@ l}], {1}]


Result:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9, 21}}

list = {{{1}, {2, 5}, {5, 10}, {10, 23}}, {{2}, {3, 35}, {4, 15}, {9,
21}}};

Map[Outer[Join, { First@#}, Rest@#, 1] &, list] // Flatten[#, 2] &


Result:

{{1, 2, 5}, {1, 5, 10}, {1, 10, 23}, {2, 3, 35}, {2, 4, 15}, {2, 9,
21}}