Function Repository Resource:

Interleave

Source Notebook

Combine multiple lists into a single list by interleaving their elements in sequence

Contributed by: Wolfram Notebook Assistant (with some help from Richard Hennigan)

ResourceFunction["Interleave"][list1,list2,]

interleaves elements from multiple lists, alternating between them in the order that the lists are provided.

Details

ResourceFunction["Interleave"] can handle lists of different lengths. The resulting list will have a length equal to the sum of the lengths of the input lists.
ResourceFunction["Interleave"] can be used with any number of lists. If only one list is provided, it simply returns that list.

Examples

Basic Examples (2) 

Interleave two lists:

In[1]:=
ResourceFunction["Interleave"][{1, 2, 3}, {4, 5, 6}]
Out[1]=

Lists of varying length:

In[2]:=
ResourceFunction["Interleave"][{1, 2, 3}, {a, b, c, d}]
Out[2]=

Scope (3) 

Interleave three lists:

In[3]:=
ResourceFunction["Interleave"][{1, 2}, {a, b, c}, {x, y, z, w}]
Out[3]=

Interleave with an empty list:

In[4]:=
ResourceFunction["Interleave"][{1, 2, 3}, {}]
Out[4]=

Interleaving a single list remains unchanged:

In[5]:=
ResourceFunction["Interleave"][{1, 2, 3}]
Out[5]=

Properties and Relations (5) 

For two lists of the same length, Interleave is equivalent to Riffle:

In[6]:=
Riffle[{1, 2, 3}, {a, b, c}]
Out[6]=
In[7]:=
ResourceFunction["Interleave"][{1, 2, 3}, {a, b, c}]
Out[7]=

If the second list is shorter than the first, Riffle will repeat elements from the second list:

In[8]:=
Riffle[{1, 2, 3, 4, 5}, {a, b}]
Out[8]=

Interleave will concatenate the remaining elements of the longer list at the end:

In[9]:=
ResourceFunction["Interleave"][{1, 2, 3, 4, 5}, {a, b}]
Out[9]=

If the first list is shorter than the second, Riffle will only use elements from the first list up to its length:

In[10]:=
Riffle[{1, 2}, {a, b, c, d, e}]
Out[10]=

Interleave will concatenate the remaining elements of the longer list at the end:

In[11]:=
ResourceFunction["Interleave"][{1, 2}, {a, b, c, d, e}]
Out[11]=

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 01 November 2024

Related Resources

License Information