Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List operations tests

Length

Returns the number of elements in a list.

$ wo 'Length[{7, 2, 4}]'
3

First

Returns the first element of a list.

$ wo 'First[{7, 2, 4}]'
7

Last

Returns the last element of a list.

$ wo 'Last[{7, 2, 4}]'
4

Rest

Returns the list without its first element.

$ wo 'Rest[{7, 2, 4}]'
{2, 4}

Most

Returns the list without its last element.

$ wo 'Most[{7, 2, 4}]'
{7, 2}

Take

Returns the first n elements of a list.

$ wo 'Take[{7, 2, 4}, 2]'
{7, 2}

Part

Returns the nth element of a list.

$ wo 'Part[{7, 6, 4}, 2]'
6

Part extraction using [[]] notation

The [[n]] notation is equivalent to Part[..., n].

$ wo '{1, 2, 3}[[2]]'
2
$ wo '{a, b, c, d}[[3]]'
c

Part extraction from variables

$ wo 'x = {10, 20, 30}; x[[2]]'
20
$ wo 'list = {a, b, c}; list[[1]]'
a

Out of bounds error

Attempting to access an index beyond the list length returns an error.

$ wo '{1, 2, 3}[[5]]'

Part::partw: Part 5 of {1, 2, 3} does not exist.
{1, 2, 3}[[5]]
$ wo 'x = {1, 2}; x[[10]]'

Part::partw: Part 10 of {1, 2} does not exist.
{1, 2}[[10]]

Drop

Returns the list without its first n elements.

$ wo 'Drop[{7, 2, 4}, 2]'
{4}

Append

Adds an element to the end of a list.

$ wo 'Append[{7, 2, 4}, 5]'
{7, 2, 4, 5}

Prepend

Adds an element to the beginning of a list.

$ wo 'Prepend[{7, 2, 4}, 5]'
{5, 7, 2, 4}

Reverse

Returns a list with elements in reverse order.

$ wo 'Reverse[{1, 2, 3}]'
{3, 2, 1}
$ wo 'Reverse[{7, 2, 4}]'
{4, 2, 7}
$ wo 'Reverse[{1}]'
{1}
$ wo 'Reverse[{}]'
{}
$ wo 'Reverse[{a, b, c, d}]'
{d, c, b, a}
$ wo 'Reverse[{-5, 3.14, 0, 42}]'
{42, 0, 3.14, -5}

Take

Returns the first n elements of a list.

$ wo 'Take[{1, 2, 3, 4, 5}, 3]'
{1, 2, 3}
$ wo 'Take[{1, 2, 3, 4, 5}, 1]'
{1}
$ wo 'Take[{1, 2, 3}, 5]'

Take::take: Cannot take positions 1 through 5 in {1, 2, 3}.
Take[{1, 2, 3}, 5]
$ wo 'Take[{a, b, c, d}, 2]'
{a, b}
$ wo 'Take[{1.5, 2.5, 3.5, 4.5}, 2]'
{1.5, 2.5}
$ wo 'Take[{10, 20, 30, 40}, 4]'
{10, 20, 30, 40}
$ wo 'Take[{1, 2, 3, 4, 5}, 5]'
{1, 2, 3, 4, 5}

Rest

Returns the list without its first element.

$ wo 'Rest[{1, 2, 3}]'
{2, 3}
$ wo 'Rest[{5, 10, 15, 20}]'
{10, 15, 20}
$ wo 'Rest[{a, b, c}]'
{b, c}
$ wo 'Rest[{42}]'
{}
$ wo 'Rest[{-5, 0, 5}]'
{0, 5}
$ wo 'Rest[{1.5, 2.5, 3.5}]'
{2.5, 3.5}

Range

Generates a sequence of numbers.

Range[n]

Generates {1, 2, …, n}.

$ wo 'Range[5]'
{1, 2, 3, 4, 5}
$ wo 'Range[1]'
{1}
$ wo 'Range[0]'
{}
$ wo 'Range[10]'
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Range[min, max]

Generates {min, min+1, …, max}.

$ wo 'Range[3, 7]'
{3, 4, 5, 6, 7}
$ wo 'Range[0, 5]'
{0, 1, 2, 3, 4, 5}
$ wo 'Range[-3, 2]'
{-3, -2, -1, 0, 1, 2}
$ wo 'Range[5, 5]'
{5}

Range[min, max, step]

Generates {min, min+step, …, max}.

$ wo 'Range[1, 10, 2]'
{1, 3, 5, 7, 9}
$ wo 'Range[0, 20, 5]'
{0, 5, 10, 15, 20}
$ wo 'Range[10, 1, -1]'
{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
$ wo 'Range[5, -5, -2]'
{5, 3, 1, -1, -3, -5}
$ wo 'Range[1, 10, 3]'
{1, 4, 7, 10}

Join

Concatenates multiple lists together.

$ wo 'Join[{a, b}, {c, d}]'
{a, b, c, d}
$ wo 'Join[{1, 2}, {3, 4}, {5, 6}]'
{1, 2, 3, 4, 5, 6}
$ wo 'Join[{1}]'
{1}
$ wo 'Join[{}]'
{}
$ wo 'Join[{}, {1, 2}]'
{1, 2}
$ wo 'Join[{1, 2}, {}]'
{1, 2}
$ wo 'Join[{a}, {b}, {c}, {d}]'
{a, b, c, d}
$ wo 'Join[{1, 2}, {3.14, 2.71}]'
{1, 2, 3.14, 2.71}

Sort

Sorts a list in ascending order.

$ wo 'Sort[{3, 1, 4, 1, 5, 9, 2, 6}]'
{1, 1, 2, 3, 4, 5, 6, 9}
$ wo 'Sort[{5, 2, 8, 1, 9}]'
{1, 2, 5, 8, 9}
$ wo 'Sort[{1}]'
{1}
$ wo 'Sort[{}]'
{}
$ wo 'Sort[{-5, 3, 0, -2, 7}]'
{-5, -2, 0, 3, 7}
$ wo 'Sort[{3.14, 2.71, 1.41, 2.23}]'
{1.41, 2.23, 2.71, 3.14}
$ wo 'Sort[{10, 5, 15, 5, 20}]'
{5, 5, 10, 15, 20}
$ wo 'Sort[{-10, -5, -15, -20}]'
{-20, -15, -10, -5}

Map

Applies a function to each element of a list.

$ wo 'Map[Sign, {-6, 0, 2, 5}]'
{-1, 0, 1, 1}

Select

Picks elements of a list that satisfy a criterion.

$ wo 'Select[{1, 2, 3, 4}, EvenQ]'
{2, 4}

Flatten

Flattens nested lists.

$ wo 'Flatten[{{1}, {2, 3}}]'
{1, 2, 3}

Total

Sums the elements of a list.

$ wo 'Total[{1, 2, 3}]'
6

Mean

Calculates the arithmetic mean (average) of a list.

$ wo 'Mean[{1, 2, 3, 4, 5}]'
3
$ wo 'Mean[{10, 20, 30}]'
20
$ wo 'Mean[{1, 2, 3}]'
2
$ wo 'Mean[{5}]'
5
$ wo 'Mean[{-5, 5}]'
0
$ wo 'Mean[{1.5, 2.5, 3.5}]'
2.5
$ wo 'Mean[{0, 0, 0, 10}]'
5/2
$ wo 'Mean[{-10, -5, 0, 5, 10}]'
0

Median

Returns the median value of a list.

$ wo 'Median[{1, 2, 3, 4, 5}]'
3
$ wo 'Median[{1, 2, 3, 4}]'
5/2
$ wo 'Median[{5, 1, 3, 2, 4}]'
3
$ wo 'Median[{10}]'
10
$ wo 'Median[{1, 10}]'
11/2
$ wo 'Median[{1, 3, 5, 7, 9}]'
5
$ wo 'Median[{-5, 0, 5}]'
0
$ wo 'Median[{1.5, 2.5, 3.5, 4.5}]'
3.
$ wo 'Median[{100, 1, 50}]'
50

Product

Evaluates the product.

$ wo 'Product[i^2, {i, 1, 6}]'
518400
$ wo 'Product[i^2, {i, 1, n}]'
n!^2

Accumulate

Returns the cumulative sums of a list.

$ wo 'Accumulate[{1, 2, 3}]'
{1, 3, 6}
$ wo 'Accumulate[{1, 2, 3, 4, 5}]'
{1, 3, 6, 10, 15}
$ wo 'Accumulate[{5}]'
{5}
$ wo 'Accumulate[{}]'
{}
$ wo 'Accumulate[{-1, 2, -3}]'
{-1, 1, -2}
$ wo 'Accumulate[{10, -5, 3}]'
{10, 5, 8}
$ wo 'Accumulate[{1.5, 2.5}]'
{1.5, 4.}
$ wo 'Accumulate[{0, 0, 1}]'
{0, 0, 1}
$ wo 'Accumulate[{-5, -10, -15}]'
{-5, -15, -30}

Differences

Returns successive differences between consecutive elements.

$ wo 'Differences[{1, 3, 6, 10}]'
{2, 3, 4}
$ wo 'Differences[{1, 2, 3, 4, 5}]'
{1, 1, 1, 1}
$ wo 'Differences[{10, 5, 3}]'
{-5, -2}
$ wo 'Differences[{5}]'
{}
$ wo 'Differences[{}]'
{}
$ wo 'Differences[{0, 1, 0, 1}]'
{1, -1, 1}
$ wo 'Differences[{1.5, 3, 5.5}]'
{1.5, 2.5}
$ wo 'Differences[{-5, -3, 0, 5}]'
{2, 3, 5}
$ wo 'Differences[{100, 90, 80}]'
{-10, -10}

Cases

Extracts elements from an expression that match a pattern.

$ wo 'Cases[{a, b, a}, a]'
{a, a}

Cases with Except pattern

$ wo 'Cases[{1, 2, 3, 4, 5}, Except[3]]'
{1, 2, 4, 5}

Cases with Alternatives

$ wo 'Cases[{1, 2, 3, 4, 5}, Except[2 | 4]]'
{1, 3, 5}

Cases with level specification

$ wo 'Cases[{{1, 2}, {3, 4}}, _Integer, {2}]'
{1, 2, 3, 4}

DeleteCases

Removes elements from an expression that match a pattern.

$ wo 'DeleteCases[{a, b, a}, a]'
{b}

MapThread

Applies a function to corresponding elements in several lists.

$ wo 'MapThread[Plus, {{1, 2}, {3, 4}}]'
{4, 6}

Partition

Breaks a list into smaller sublists.

$ wo 'Partition[{1, 2, 3, 4}, 2]'
{{1, 2}, {3, 4}}

SortBy

Sorts elements of a list according to a function.

$ wo 'SortBy[{3, 1, 2}, # &]'
{1, 2, 3}

GroupBy

Groups elements of a list according to a function.

$ wo 'GroupBy[{{a, b}, {a, c}, {b, c}}, First]'
<|a -> {{a, b}, {a, c}}, b -> {{b, c}}|>

Array

Constructs an array using a function to generate elements.

$ wo 'Array[#^2 &, 3]'
{1, 4, 9}

Table

Generates a list by evaluating an expression for different values of a variable.

Table[expr, n]

Generates a list of n copies of expr.

$ wo 'Table[x, 3]'
{x, x, x}
$ wo 'Table[1, 5]'
{1, 1, 1, 1, 1}

Table[expr, {i, max}]

Generates a list where i goes from 1 to max.

$ wo 'Table[i^2, {i, 5}]'
{1, 4, 9, 16, 25}
$ wo 'Table[i, {i, 4}]'
{1, 2, 3, 4}
$ wo 'Table[2*i, {i, 3}]'
{2, 4, 6}

Table[expr, {i, min, max}]

Generates a list where i goes from min to max.

$ wo 'Table[i, {i, 3, 7}]'
{3, 4, 5, 6, 7}
$ wo 'Table[i^2, {i, 0, 4}]'
{0, 1, 4, 9, 16}
$ wo 'Table[i, {i, -2, 2}]'
{-2, -1, 0, 1, 2}

Table[expr, {i, min, max, step}]

Generates a list where i goes from min to max in steps.

$ wo 'Table[i, {i, 1, 10, 2}]'
{1, 3, 5, 7, 9}
$ wo 'Table[i, {i, 10, 1, -2}]'
{10, 8, 6, 4, 2}

Position

Finds all positions of a pattern in a list.

$ wo 'Position[{a, b, a, c, a}, a]'
{{1}, {3}, {5}}
$ wo 'Position[{1, 2, 3, 2, 1}, 2]'
{{2}, {4}}
$ wo 'Position[{1, 2, 3}, 4]'
{}
$ wo 'Position[{x, y, x, z}, x]'
{{1}, {3}}

Count

Counts the number of occurrences of a pattern in a list.

$ wo 'Count[{a, b, a, c, a}, a]'
3
$ wo 'Count[{1, 2, 3, 2, 1}, 2]'
2
$ wo 'Count[{1, 2, 3}, 4]'
0
$ wo 'Count[{x, x, x, y}, x]'
3

DeleteDuplicates

Removes duplicate elements from a list.

$ wo 'DeleteDuplicates[{a, b, a, c, b}]'
{a, b, c}
$ wo 'DeleteDuplicates[{1, 2, 1, 3, 2}]'
{1, 2, 3}
$ wo 'DeleteDuplicates[{1, 1, 1}]'
{1}
$ wo 'DeleteDuplicates[{a}]'
{a}
$ wo 'DeleteDuplicates[{}]'
{}

Union

Returns the sorted union of lists (removes duplicates).

$ wo 'Union[{1, 2, 3}, {2, 3, 4}]'
{1, 2, 3, 4}
$ wo 'Union[{a, b}, {b, c}]'
{a, b, c}
$ wo 'Union[{3, 1, 2}]'
{1, 2, 3}
$ wo 'Union[{1, 1, 2, 2}]'
{1, 2}
$ wo 'Union[{1, 2}, {3, 4}, {5, 6}]'
{1, 2, 3, 4, 5, 6}

Intersection

Returns the sorted intersection of lists.

$ wo 'Intersection[{1, 2, 3, 4}, {2, 4, 6}]'
{2, 4}
$ wo 'Intersection[{a, b, c}, {b, c, d}]'
{b, c}
$ wo 'Intersection[{1, 2, 3}, {4, 5, 6}]'
{}
$ wo 'Intersection[{1, 2, 3}, {1, 2, 3}]'
{1, 2, 3}
$ wo 'Intersection[{1, 2, 3}, {2, 3}, {3}]'
{3}

Complement

Returns elements in the first list but not in any of the other lists.

$ wo 'Complement[{1, 2, 3, 4, 5}, {2, 4}]'
{1, 3, 5}
$ wo 'Complement[{a, b, c, d}, {b, d}]'
{a, c}
$ wo 'Complement[{1, 2, 3}, {1, 2, 3}]'
{}
$ wo 'Complement[{1, 2, 3, 4}, {2}, {4}]'
{1, 3}

ConstantArray

Creates an array of repeated elements.

$ wo 'ConstantArray[x, 3]'
{x, x, x}
$ wo 'ConstantArray[0, 5]'
{0, 0, 0, 0, 0}
$ wo 'ConstantArray[1, 0]'
{}
$ wo 'ConstantArray[a, {2, 3}]'
{{a, a, a}, {a, a, a}}

Tally

Counts occurrences of each distinct element.

$ wo 'Tally[{a, b, a, c, b, a}]'
{{a, 3}, {b, 2}, {c, 1}}
$ wo 'Tally[{1, 2, 1, 3}]'
{{1, 2}, {2, 1}, {3, 1}}
$ wo 'Tally[{x, x, x}]'
{{x, 3}}
$ wo 'Tally[{}]'
{}

ReplacePart

Replaces element at a specific position.

$ wo 'ReplacePart[{a, b, c}, 2 -> x]'
{a, x, c}
$ wo 'ReplacePart[{1, 2, 3, 4}, 1 -> 0]'
{0, 2, 3, 4}
$ wo 'ReplacePart[{a, b, c}, -1 -> z]'
{a, b, z}

MinMax

Returns the minimum and maximum of a list.

$ wo 'MinMax[{3, 1, 4, 1, 5, 9}]'
{1, 9}
$ wo 'MinMax[{-5, 0, 5}]'
{-5, 5}
$ wo 'MinMax[{42}]'
{42, 42}

PadLeft

Pads a list on the left to a specified length.

$ wo 'PadLeft[{1, 2, 3}, 5]'
{0, 0, 1, 2, 3}
$ wo 'PadLeft[{a, b}, 4, x]'
{x, x, a, b}
$ wo 'PadLeft[{1, 2, 3, 4, 5}, 3]'
{3, 4, 5}

PadRight

Pads a list on the right to a specified length.

$ wo 'PadRight[{1, 2, 3}, 5]'
{1, 2, 3, 0, 0}
$ wo 'PadRight[{a, b}, 4, x]'
{a, b, x, x}
$ wo 'PadRight[{1, 2, 3, 4, 5}, 3]'
{1, 2, 3}

RotateLeft

Rotates list elements to the left.

$ wo 'RotateLeft[{1, 2, 3, 4, 5}]'
{2, 3, 4, 5, 1}
$ wo 'RotateLeft[{a, b, c, d}, 2]'
{c, d, a, b}
$ wo 'RotateLeft[{1, 2, 3}, 0]'
{1, 2, 3}

RotateRight

Rotates list elements to the right.

$ wo 'RotateRight[{1, 2, 3, 4, 5}]'
{5, 1, 2, 3, 4}
$ wo 'RotateRight[{a, b, c, d}, 2]'
{c, d, a, b}
$ wo 'RotateRight[{1, 2, 3}, 0]'
{1, 2, 3}

Riffle

Inserts an element between each pair of elements.

$ wo 'Riffle[{a, b, c}, x]'
{a, x, b, x, c}
$ wo 'Riffle[{1, 2, 3, 4}, 0]'
{1, 0, 2, 0, 3, 0, 4}
$ wo 'Riffle[{a}, x]'
{a}
$ wo 'Riffle[{}, x]'
{}

Element-wise interleaving with a second list:

$ wo 'Riffle[{a, b, c, d}, {1, 2, 3, 4}]'
{a, 1, b, 2, c, 3, d, 4}
$ wo 'Riffle[{a, b, c}, {1, 2}]'
{a, 1, b, 2, c}

AnyTrue

Checks if any element satisfies a predicate.

$ wo 'AnyTrue[{1, 2, 3, 4}, EvenQ]'
True
$ wo 'AnyTrue[{1, 3, 5}, EvenQ]'
False
$ wo 'AnyTrue[{1, 2, 3}, # > 2 &]'
True

NoneTrue

Checks if no element satisfies a predicate.

$ wo 'NoneTrue[{1, 3, 5}, EvenQ]'
True
$ wo 'NoneTrue[{1, 2, 3}, EvenQ]'
False
$ wo 'NoneTrue[{1, 2, 3}, # > 5 &]'
True

Transpose

Transposes a matrix (list of lists).

$ wo 'Transpose[{{1, 2}, {3, 4}}]'
{{1, 3}, {2, 4}}
$ wo 'Transpose[{{a, b, c}, {d, e, f}}]'
{{a, d}, {b, e}, {c, f}}
$ wo 'Transpose[{{1, 2, 3}}]'
{{1}, {2}, {3}}

Thread

Threads a function over corresponding list elements.

$ wo 'Thread[f[{a, b}, {x, y}]]'
{f[a, x], f[b, y]}
$ wo 'Thread[Plus[{1, 2}, {3, 4}]]'
{4, 6}
$ wo 'Thread[g[{a, b, c}, {1, 2, 3}]]'
{g[a, 1], g[b, 2], g[c, 3]}

Gather

Groups identical elements together, maintaining order of first appearance.

$ wo 'Gather[{1, 1, 2, 2, 1}]'
{{1, 1, 1}, {2, 2}}
$ wo 'Gather[{a, b, a, c, b}]'
{{a, a}, {b, b}, {c}}

GatherBy

Groups elements by applying a function, maintaining order of first appearance.

$ wo 'GatherBy[{1, 2, 3, 4, 5}, EvenQ]'
{{1, 3, 5}, {2, 4}}
$ wo 'GatherBy[{-2, -1, 0, 1, 2}, Sign]'
{{-2, -1}, {0}, {1, 2}}

Split

Splits list at boundaries where consecutive elements differ.

$ wo 'Split[{1, 1, 2, 2, 3}]'
{{1, 1}, {2, 2}, {3}}
$ wo 'Split[{a, a, b, c, c, c}]'
{{a, a}, {b}, {c, c, c}}

SplitBy

Splits list at boundaries where a function changes value.

$ wo 'SplitBy[{1, 2, 3, 4, 5}, EvenQ]'
{{1}, {2}, {3}, {4}, {5}}

FreeQ

Tests if expression is free of a specified form.

$ wo 'FreeQ[{1, 2, 3}, 4]'
True
$ wo 'FreeQ[{1, 2, 3}, 2]'
False

Extract

Extracts parts at specified positions.

$ wo 'Extract[{a, b, c, d}, 2]'
b
$ wo 'Extract[{a, {b1, b2, b3}, c, d}, {2, 3}]'
b3

Catenate

Flattens one level of lists.

$ wo 'Catenate[{{1, 2}, {3, 4}}]'
{1, 2, 3, 4}
$ wo 'Catenate[{{a, b}, {c}, {d, e, f}}]'
{a, b, c, d, e, f}

TakeWhile

Takes elements from the start while a predicate is true.

$ wo 'TakeWhile[{1, 2, 3, 4, 5}, # < 4 &]'
{1, 2, 3}
$ wo 'TakeWhile[{2, 4, 6, 7, 8}, EvenQ]'
{2, 4, 6}

Apply

Applies a function to list elements as arguments.

$ wo 'Apply[Plus, {1, 2, 3}]'
6
$ wo 'Apply[Times, {2, 3, 4}]'
24

Identity

Returns its argument unchanged.

$ wo 'Identity[5]'
5
$ wo 'Identity[{1, 2, 3}]'
{1, 2, 3}

Outer

Generalized outer product - applies function to all pairs.

$ wo 'Outer[Times, {1, 2}, {3, 4}]'
{{3, 4}, {6, 8}}
$ wo 'Outer[Plus, {1, 2}, {10, 20}]'
{{11, 21}, {12, 22}}

Inner

Generalized inner product (like dot product).

$ wo 'Inner[Times, {1, 2, 3}, {4, 5, 6}, Plus]'
32
$ wo 'Inner[Plus, {1, 2}, {3, 4}, Times]'
24

Permutations

Generates all permutations of a list.

$ wo 'Permutations[{a, b, c}]'
{{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}}
$ wo 'Permutations[{1, 2, 3}, {2}]'
{{1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}}
$ wo 'Permutations[{1, 2}, {1}]'
{{1}, {2}}
$ wo 'Permutations[{a}]'
{{a}}
$ wo 'Permutations[{}]'
{{}}
$ wo 'Length[Permutations[Range[4]]]'
24

Subsets

Generates subsets (combinations) of a list.

$ wo 'Subsets[{a, b, c}]'
{{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
$ wo 'Subsets[{a, b, c}, {2}]'
{{a, b}, {a, c}, {b, c}}
$ wo 'Subsets[{a, b, c}, {0}]'
{{}}
$ wo 'Subsets[{1, 2, 3, 4}, {3}]'
{{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}}

SparseArray

Creates a matrix from position-value rules with a default fill value.

$ wo 'Normal[SparseArray[{{1, 2} -> "Q", {3, 1} -> "Q"}, {3, 3}, "."]]'
{{., Q, .}, {., ., .}, {Q, ., .}}
$ wo 'Normal[SparseArray[{{1, 2} -> "Q"}, {2, 2}, "."]]'
{{., Q}, {., .}}

Table (multi-dimensional)

Table supports multiple iterator specifications for multi-dimensional arrays.

$ wo 'Table[i + j, {i, 1, 2}, {j, 1, 3}]'
{{2, 3, 4}, {3, 4, 5}}
$ wo 'Table["x", {3}]'
{x, x, x}
$ wo 'Table[0, {3}, {2}]'
{{0, 0}, {0, 0}, {0, 0}}