Skip to content

PadRight ​

Pad a list on the right to length n, or truncate it from the right.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguagePadRight✓
PadRight(list)a ragged array (a list of lists) padded with 0s to a full matrix.
PadRight(list, n, x?)padded on the right with xx (default 0) to length nn; a shorter nn drops elements from the right instead.
Typed signatures
TypePackageApplies
(list<any>, integer?, any?) -> collectioncombinatoricsalways

Domain: Collections

Examples

Basic #

Padded with zeros on the right

live ↗
InPadRight([1,2,3],5)\mathrm{PadRight}(\bigl\lbrack1, 2, 3\bigr\rbrack, 5)Out[1,2,3,0,0]\bigl\lbrack1, 2, 3, 0, 0\bigr\rbrack

A padding element

live ↗
InPadRight([1,2,3],5,x)\mathrm{PadRight}(\bigl\lbrack1, 2, 3\bigr\rbrack, 5, x)Out[1,2,3,x,x]\bigl\lbrack1, 2, 3, x, x\bigr\rbrack

Pads on the right with zeros

live ↗
InPadRight([a,b,c],10)\mathrm{PadRight}(\bigl\lbrack a, b, c\bigr\rbrack, 10)Out[a,b,c,0,0,0,0,0,0,0]\bigl\lbrack a, b, c, 0, 0, 0, 0, 0, 0, 0\bigr\rbrack

A constant padding element

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

A padding list repeats cyclically after the data

live ↗
InPadRight([a,b,c],10,[x,y,z])\mathrm{PadRight}(\bigl\lbrack a, b, c\bigr\rbrack, 10, \bigl\lbrack x, y, z\bigr\rbrack)Out[a,b,c,x,y,z,x,y,z,x]\bigl\lbrack a, b, c, x, y, z, x, y, z, x\bigr\rbrack
Scope #

A shorter length drops elements from the right

live ↗
InPadRight([1,2,3,4,5],3)\mathrm{PadRight}(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack, 3)Out[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack

With no length, a ragged array is padded to a full matrix

live ↗
InPadRight([[1],[2,3]])\mathrm{PadRight}(\bigl\lbrack\bigl\lbrack1\bigr\rbrack, \bigl\lbrack2, 3\bigr\rbrack\bigr\rbrack)Out[[1,0],[2,3]]\bigl\lbrack\bigl\lbrack1, 0\bigr\rbrack, \bigl\lbrack2, 3\bigr\rbrack\bigr\rbrack

A negative length pads on the left instead

live ↗
InPadRight([a,b,c],−10)\mathrm{PadRight}(\bigl\lbrack a, b, c\bigr\rbrack, -10)Out[0,0,0,0,0,0,0,a,b,c]\bigl\lbrack0, 0, 0, 0, 0, 0, 0, a, b, c\bigr\rbrack
Possible issues #

A nested padding list is used whole as the padding element

live ↗
InPadRight([a,b,c],7,[[u,v]])\mathrm{PadRight}(\bigl\lbrack a, b, c\bigr\rbrack, 7, \bigl\lbrack\bigl\lbrack u, v\bigr\rbrack\bigr\rbrack)Out[a,b,c,[u,v],[u,v],[u,v],[u,v]]\bigl\lbrack a, b, c, \bigl\lbrack u, v\bigr\rbrack, \bigl\lbrack u, v\bigr\rbrack, \bigl\lbrack u, v\bigr\rbrack, \bigl\lbrack u, v\bigr\rbrack\bigr\rbrack

The cycle is positioned relative to the data, so it begins with vv

live ↗
InPadRight([a,b,c],7,[u,v])\mathrm{PadRight}(\bigl\lbrack a, b, c\bigr\rbrack, 7, \bigl\lbrack u, v\bigr\rbrack)Out[a,b,c,v,u,v,u]\bigl\lbrack a, b, c, v, u, v, u\bigr\rbrack
About
Examples10
Checked againstWolfram Language 10 agree
CrosswalkWolfram Language

See also: PadLeft, Take