# How to Riffle lists without cyclic repetitions

To the follow to my previous question How to combine a few lists?.

There are two lists: a={1,2,4,5,7,6,9,2}, b={1,3,4} I would like to get the next lists:

1. ab1={1,1,2,3,4,4,5,7,6,9,2} (the first element of b is placed between the first and second elements of a, the second element of b is placed between the second and third elements of a, etc)

2. ab2={1,2,4,5,7,1,6,3,9,4,2} (similar to 1., but put the elements of the list b from the end of the list a)

3. ab3={1,2,1,4,3,5,4,7,6,9,2} (in this case elements of b can be placed in a specific place of the list a, in particular here the first element of b is placed between the second and third elements of the list a, etc )

• As Flatten may be used to Transpose a 'ragged' array, (Flatten[{a,b},{{2}}]//Flatten)==ab1 May 5, 2023 at 9:07

Using PadRight:

First, the explanation:

PadRight[{a, b}, {2, Max[{Length@a, Length@b}]}, \[Wolf]] // Grid


Keep reading from top to bottom and across. At the end, delete the symbol that was introduced. This prevents the cyclic riffle effect.

(PadRight[{a, b}, {2, Max[{Length@a, Length@b}]}, \[Wolf]] //
Transpose // Flatten) /. \[Wolf] -> Nothing


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

Second case is just the reversal of the idea. Don't forget to reverse the generated list at the end.

(PadRight[{Reverse@a, Reverse@b}, {2,
Max[{Length@a, Length@b}]}, \[Wolf]] // Transpose //
Flatten) /. \[Wolf] -> Nothing // Reverse


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

For the third case, PadRight provides an additional parameter that can be used to pad elements at the left. Do so with the second list only so as to shift it right by one.

({PadRight[a, Max[{Length@a, Length@b}], \[Wolf]]
, PadRight[b, Max[{Length@a, Length@b}], \[Wolf], 1]
} // Transpose // Flatten) /. \[Wolf] -> Nothing


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

Now I have to invest time in learning about Riffle for which two excellent answers already exist.

• Thanks for the interesting explanation! May 5, 2023 at 8:33

You may use "Riffle" like:

1. First element of b at place 2:

a = {1, 2, 4, 5, 7, 6, 9, 2}; b = {1, 3, 4};

Riffle[a, b, {2, 2 Length[b], 2}]

2. Last element of b at second to last place:

Riffle[a, b, {Length[a] - Length[b] + 1, Length[a] + Length[b], 2}]

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

1. First element of b at position 3;

Riffle[a, b, {3, (Length[a] + Length[b]) - 3, 2}]

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

• Thank you very much! May 5, 2023 at 7:47
riffle[a_, b_, start_, step_] := Riffle[a, b,
Append[step] @
If[Positive @ start, Identity, Reverse] @
(start + {0, Sign[start] step (Length @ b - 1)})]


Examples:

a = {1,2,4,5,7,6,9,2};
b = {1,3,4};

riffle[a, b, 2, 2] == ab1

True

riffle[a, b, -2, 2] == ab2

True

riffle[a, b, 3, 2] == ab3

True

bb = {b1, b2, b3};

riffle[a, bb, 2, 2]

{1, b1, 2, b2, 4, b3, 5, 7, 6, 9, 2}

riffle[a, bb, 3, 2]

{1, 2, b1, 4, b2, 5, b3, 7, 6, 9, 2}

riffle[a, bb, 2, 3]

{1, b1, 2, 4, b2, 5, 7, b3, 6, 9, 2}

riffle[a, bb, -2, 3]

{1, 2, 4, b1, 5, 7, b2, 6, 9, b3, 2}

• Thank you very much! May 5, 2023 at 7:47