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
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]'
{1, 2, 3}
$ 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}]'
2.5
$ 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}]'
2.5
$ wo 'Median[{5, 1, 3, 2, 4}]'
3
$ wo 'Median[{10}]'
10
$ wo 'Median[{1, 10}]'
5.5
$ 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
Multiplies all the elements of a list together.
$ wo 'Product[{1, 2, 3}]'
6
$ wo 'Product[{2, 3, 4}]'
24
$ wo 'Product[{1, 2, 3, 4, 5}]'
120
$ wo 'Product[{5}]'
5
$ wo 'Product[{}]'
1
$ wo 'Product[{-2, 3}]'
-6
$ wo 'Product[{-2, -3}]'
6
$ wo 'Product[{2, 0, 5}]'
0
$ wo 'Product[{1.5, 2}]'
3
$ wo 'Product[{2, 2.5}]'
5
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}
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}}|>/
Accumulate
Returns the cumulative sums of a list.
$ wo 'Accumulate[{1, 2, 3}]'
{1, 3, 6}
Array
Constructs an array using a function to generate elements.
$ wo 'Array[#^2 &, 3]'
{1, 4, 9}