Wolfram: Riffle (Add at Every Nth)

By Xah Lee. Date: .

Riffle. (Intersperse. Add at Every Nth)

Riffle
  • Riffle[ list, x ]
  • Riffle[ list, x, n ]
  • Riffle[ list, list2 ]

intersperse x into the list, at every nth (default 2). Or intersperse 2 lists list, list2 .

Riffle[ {1, 2, 3}, x ]
(* {1, x, 2, x, 3} *)

Riffle[ {1, 2, 3}, x, 2 ]
(* {1, x, 2, x, 3} *)

Riffle[ {1, 2, 3, 4, 5, 6}, x, 3 ]
(* {1, 2, x, 3, 4, x, 5, 6} *)
Riffle[ {1, 2, 3}, {a, b, c} ]
(* {1, a, 2, b, 3, c} *)

Wolfram. List Operations, and Loop, Iteration, Recursion