Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > List Manipulation > Rearranging & Restructuring Lists >
Mathematica > Data Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists >

Riffle

Riffle[{e1, e2, ...}, x]
gives {e1, x, e2, x, ...}.
Riffle[{e1, e2, ...}, {x1, x2, ...}]
gives {e1, x1, e2, x2, ...}.
Riffle[list, x, n]
yields a list in which every n^(th) element is x.
Riffle[list, x, {imin, imax, n}]
yields a list in which x appears if possible at positions imin, imin+n, imin+2n, ... , imax.
  • If there are fewer xi than gaps between ei in Riffle[{e1, e2, ...}, {x1, x2, ...}], the xi are used cyclically.
  • The specification {imin, imax, n} is of the type used in Take. Negative indices count from the end of the list.
  • Riffle[list, x] is equivalent to Riffle[list, x, {2, -2, 2}].
  • Riffle[list, x, n] is equivalent to Riffle[list, x, {n, -2, n}].
  • In Riffle[list, xlist], if list and xlist are of the same length, then their elements are directly interleaved, so that the last element of the result is the last element of xlist.
Riffle x between successive elements in a list:
Riffle in x and y cyclically:
Riffle in x at every 3^(rd) position:
Riffle x between successive elements in a list:
In[1]:=
Click for copyable input
Out[1]=
 
Riffle in x and y cyclically:
In[1]:=
Click for copyable input
Out[1]=
 
Riffle in x at every 3^(rd) position:
In[1]:=
Click for copyable input
Out[1]=
Riffle in x at every other position:
Include x at the beginning and end:
Use step 3:
Start riffling in x only at position 5:
Cyclically riffle in x and y:
Intersperse two lists:
Riffle works on SparseArray objects:
Insert spaces between words:
Create a directory name from a path list:
Insert 0s between successive integers:
Alternate positive and negative integers:
Insert commas at every 4^(th) character:
New in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team