# Manipulating Lists

Constructing Lists | Partitioning and Padding Lists |

Manipulating Lists by Their Indices | Sparse Arrays: Manipulating Lists |

Nested Lists |

Range[n] | the list {1,2,3,…,n} |

Table[expr,{i,n}] | the values of expr with i from 1 to n |

Array[f,n] | the list {f[1],f[2],…,f[n]} |

NestList[f,x,n] | {x,f[x],f[f[x]],…} with up to n nestings |

Normal[SparseArray[{i_{1}->v_{1},…},n]] | a length n list with element i _{k} being v_{k} |

Apply[List,f[e_{1},e_{2},…]] | the list {e _{1},e_{2},…} |

Table[expr,{i,list}] | the values of expr with i taking on values from list |

Map[f,list] | apply f to each element of list |

MapIndexed[f,list] | give f[elem,{i}] for the i ^{th }element |

Cases[list,form] | give elements of list that match form |

Select[list,test] | select elements for which test[elem] is True |

Pick[list,sel,form] | pick out elements of list for which the corresponding elements of sel match form |

TakeWhile[list,test] | |

list[[{i_{1},i_{2},…}]] or Part[list,{i_{1},i_{2},…}] | |

give a list of the specified parts of list |

Part[list,spec] or list[[spec]] | part or parts of a list |

Part[list,spec_{1},spec_{2},…] or list[[spec_{1},spec_{2},…]] | part or parts of a nested list |

n | the n ^{th} part from the beginning |

-n | the n ^{th} part from the end |

{i_{1},i_{2},…} | a list of parts |

m;;n | parts m through n |

All | all parts |

_{1},spec

_{2},…]]. If a given spec

_{k}is a single integer, then it represents extracting a single slice in the k

^{th}dimension, while if it is a list, it represents extracting a list of parallel slices. The final result for list[[spec

_{1},spec

_{2},…]] is then the collection of elements obtained by slicing in each successive dimension.

Part[list,{i_{1},i_{2},…}] | the list {list[[i _{1}]],list[[i_{2}]],…} |

Extract[list,{i_{1},i_{2},…}] | the element list[[i _{1},i_{2},…]] |

Part[list,spec_{1},spec_{2},…] | parts specified by successive slicing |

Extract[list,{{i_{1},i_{2},…},{j_{1},j_{2},…},…}] | the list of individual parts {list[[i _{1},i_{2},…]],list[[j_{1},j_{2},…]],…} |

Take[list,spec] | take the specified parts of a list |

Drop[list,spec] | drop the specified parts of a list |

Take[list,spec_{1},spec_{2},…]
,
Drop[list,spec_{1},spec_{2},…] | take or drop specified parts at each level in nested lists |

n | the first n elements |

-n | the last n elements |

{n} | element n only |

{m,n} | elements m through n (inclusive) |

{m,n,s} | elements m through n in steps of s |

All | all parts |

None | no parts |

Prepend[list,elem] | add element at the beginning of list |

Append[list,elem] | add element at the end of list |

Insert[list,elem,i] | insert element at position i |

Insert[list,elem,{i,j,…}] | insert at position {i,j,…} |

Delete[list,i] | delete the element at position i |

Delete[list,{i,j,…}] | delete at position {i,j,…} |

ReplacePart[list,i->new] | replace the element at position i in list with new |

ReplacePart[list,{i,j,…}->new] | replace list[[i,j,…]] with new |

ReplacePart[list,{i_{1}->new_{1},i_{2}->new_{2},…}] | replaces parts at positions i _{n} by new_{n} |

ReplacePart[list,{{i_{1},j_{1},…}->new_{1},…}] | replace parts at positions {i _{n},j_{n},…} by new_{n} |

ReplacePart[list,{{i_{1},j_{1},…},…}->new] | replace all parts list[[i _{k},j_{k},…]] with new |

{list_{1},list_{2},…} | list of lists |

Table[expr,{i,m},{j,n},…] | m×n×… table of values of expr |

Array[f,{m,n,…}] | m×n×… array of values f[i,j,…] |

Normal[SparseArray[{{i_{1},j_{1},…}->v_{1},…},{m,n,…}]] | |

m×n×… array with element {i _{s},j_{s},…} being v_{s} | |

Outer[f,list_{1},list_{2},…] | generalized outer product with elements combined using f |

Tuples[list,{m,n,…}] | all possible m×n×… arrays of elements from list |

*full arrays*, in which all sublists at a particular level are the same length.

Dimensions[list] | the dimensions of a full array |

ArrayQ[list] | test whether all sublists at a given level are the same length |

ArrayDepth[list] | the depth to which all sublists are the same length |

Flatten[list] | flatten out all levels of list |

Flatten[list,n] | flatten out the top n levels |

ArrayFlatten[list,rank] | create a flattened array from an array of arrays |

Transpose[list] | transpose the top two levels of list |

Transpose[list,{n_{1},n_{2},…}] | put the k ^{th} level in list at level n_{k} |

Map[f,list,{n}] | map f across elements at level n |

Apply[f,list,{n}] | apply f to the elements at level n |

MapIndexed[f,list,{n}] | map f onto parts at level n and their indices |

Partition[list,{n_{1},n_{2},…}] | partition into n _{1}×n_{1}×… blocks |

PadLeft[list,{n_{1},n_{2},…}] | pad on the left to make an n _{1}×n_{1}×… array |

PadRight[list,{n_{1},n_{2},…}] | pad on the right to make an n _{1}×n_{1}×… array |

RotateLeft[list,{n_{1},n_{2},…}] | rotate n _{k} places to the left at level k |

RotateRight[list,{n_{1},n_{2},…}] | rotate n _{k} places to the right at level k |

Partition[list,n] | partition list into sublists of length n |

Partition[list,n,d] | partition into sublists with offset d |

Split[list] | split list into runs of identical elements |

Split[list,test] | split into runs with adjacent elements satisfying test |

Partition[list,n,d] or Partition[list,n,d,{1,-1}] | keep only sublists with no overhangs |

Partition[list,n,d,{1,1}] | allow an overhang at the end |

Partition[list,n,d,{-1,-1}] | allow an overhang at the beginning |

Partition[list,n,d,{-1,1}] | allow overhangs at both the beginning and end |

Partition[list,n,d,{k_{L},k_{R}}] | specify alignments of first and last sublists |

Partition[list,n,d,spec] | pad by cyclically repeating elements in list |

Partition[list,n,d,spec,x] | pad by repeating the element x |

Partition[list,n,d,spec,{x_{1},x_{2},…}] | |

pad by cyclically repeating the x _{i} | |

Partition[list,n,d,spec,{}] | use no padding |

_{L},k

_{R}} tells Partition to give the sequence of sublists in which the first element of the original list appears at position in the first sublist, and the last element of the original list appears at position in the last sublist.

PadLeft[list,n] | pad to length n by inserting zeros on the left |

PadLeft[list,n,x] | pad by repeating the element x |

PadLeft[list,n,{x_{1},x_{2},…}] | pad by cyclically repeating the x _{i} |

PadLeft[list,n,list] | pad by cyclically repeating list |

PadLeft[list,n,padding,m] | leave a margin of m elements on the right |

PadRight[list,n] | pad by inserting zeros on the right |

{e_{1},e_{2},…}
,
{{e_{11},e_{12},…},…}
,
… | ordinary lists |

SparseArray[{pos_{1}->val_{1},pos_{2}->val_{2},…}] | |

sparse arrays |

SparseArray[list] | sparse array version of list |

SparseArray[{pos_{1}->val_{1},pos_{2}->val_{2},…}] | |

sparse array with values val _{i} at positions pos_{i} | |

SparseArray[{pos_{1},pos_{2},…}->{val_{1},val_{2},…}] | |

the same sparse array | |

SparseArray[Band[{i,j}]->val] | banded sparse array with values val |

SparseArray[data,{d_{1},d_{2},…}] | d _{1}×d_{2}×… sparse array |

SparseArray[data,dims,val] | sparse array with default value val |

Normal[array] | ordinary list version of array |

ArrayRules[array] | position-value rules for array |