Skip to content

Riffle ​

Interleave a separator (or a second list) between the elements of a list.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageRiffle✓
Riffle(list, x)xx between every consecutive pair of elements; if xx is itself a ListList, its elements are used in turn (and, once list and x run the same length, a trailing one too).
Riffle(list, x, n)a separator every nn elements, rather than between every pair.
Typed signatures
TypePackageApplies
(list<any>, any, integer?) -> collectioncombinatoricsalways

Domain: Collections

Examples

Basic #

xx between consecutive elements

live ↗
InRiffle([a,b,c],x)\mathrm{Riffle}(\bigl\lbrack a, b, c\bigr\rbrack, x)Out[a,x,b,x,c]\bigl\lbrack a, x, b, x, c\bigr\rbrack

Interleave two lists of the same length

live ↗
InRiffle([a,b,c],[x,y,z])\mathrm{Riffle}(\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack x, y, z\bigr\rbrack)Out[a,x,b,y,c,z]\bigl\lbrack a, x, b, y, c, z\bigr\rbrack

Inserts xx between every adjacent pair

live ↗
InRiffle([1,2,3,4,5,6,7,8,9],x)\mathrm{Riffle}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8, 9\bigr\rbrack, x)Out[1,x,2,x,3,x,4,x,5,x,6,x,7,x,8,x,9]\bigl\lbrack1, x, 2, x, 3, x, 4, x, 5, x, 6, x, 7, x, 8, x, 9\bigr\rbrack

Inserts xx after every third element

live ↗
InRiffle([1,2,3,4,5,6,7,8,9],x,3)\mathrm{Riffle}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8, 9\bigr\rbrack, x, 3)Out[1,2,x,3,4,x,5,6,x,7,8,x,9]\bigl\lbrack1, 2, x, 3, 4, x, 5, 6, x, 7, 8, x, 9\bigr\rbrack

A list of fillers alternates between them

live ↗
InRiffle([1,2,3,4,5,6,7,8,9],[x,y])\mathrm{Riffle}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7, 8, 9\bigr\rbrack, \bigl\lbrack x, y\bigr\rbrack)Out[1,x,2,y,3,x,4,y,5,x,6,y,7,x,8,y,9]\bigl\lbrack1, x, 2, y, 3, x, 4, y, 5, x, 6, y, 7, x, 8, y, 9\bigr\rbrack
Scope #

A list one shorter goes strictly between the elements

live ↗
InRiffle([a,b,c],[x,y])\mathrm{Riffle}(\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack x, y\bigr\rbrack)Out[a,x,b,y,c]\bigl\lbrack a, x, b, y, c\bigr\rbrack

Every 3rd element of the result is x

live ↗
InRiffle([1,2,3,4,5,6,7],x,3)\mathrm{Riffle}(\bigl\lbrack1, 2, 3, 4, 5, 6, 7\bigr\rbrack, x, 3)Out[1,2,x,3,4,x,5,6,x,7]\bigl\lbrack1, 2, x, 3, 4, x, 5, 6, x, 7\bigr\rbrack

A single element has nothing to separate

live ↗
InRiffle([a],x)\mathrm{Riffle}(\bigl\lbrack a\bigr\rbrack, x)Out[a]\bigl\lbrack a\bigr\rbrack

With equal lengths, the lists interleave and the last filler is appended

live ↗
InRiffle([a,b,c,d],[x,y,z,w])\mathrm{Riffle}(\bigl\lbrack a, b, c, d\bigr\rbrack, \bigl\lbrack x, y, z, w\bigr\rbrack)Out[a,x,b,y,c,z,d,w]\bigl\lbrack a, x, b, y, c, z, d, w\bigr\rbrack
Applications #

Zeros between consecutive integers

live ↗
InRiffle(1..10,0)\mathrm{Riffle}(1..10, 0)Out[1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10]\bigl\lbrack1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10\bigr\rbrack

Interleaves the integers with their negatives

live ↗
InRiffle(1..10,−(1..10))\mathrm{Riffle}(1..10, -(1..10))Out[1,−1,2,−2,3,−3,4,−4,5,−5,6,−6,7,−7,8,−8,9,−9,10,−10]\bigl\lbrack1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9, 10, -10\bigr\rbrack
Properties #

Like joining with a separator: none at the ends

live ↗
InRiffle([a,b,c,d],x)\mathrm{Riffle}(\bigl\lbrack a, b, c, d\bigr\rbrack, x)Out[a,x,b,x,c,x,d]\bigl\lbrack a, x, b, x, c, x, d\bigr\rbrack
About
Examples12
Checked againstWolfram Language 12 agree
CrosswalkWolfram Language

See also: Join, Partition