lists - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Sets and Lists

 Calling Sequence {es} [es]

Parameters

 es - expression sequence

Description

 • A set is an unordered sequence of distinct expressions enclosed in braces {}, representing a set in the mathematical sense.
 • A list is an ordered sequence of expressions enclosed in square brackets []. The ordering of the expressions is the ordering of es.
 • Note that es may be empty so that the empty set is represented by {} and the empty list is represented by [].
 • In output, the empty set is typeset as $\varnothing$.
 • The number of elements of a set or list S is given by numelems(S).

Accessing the elements

 • The elements of a set or list can be extracted via the selection operation. Thus if S is a set or list then S[i] selects the ith element.  Alternatively you may use op(i,S).  The elements are numbered from 1 so S[1] extracts the first element. Negative subscripts can also be used.  S[-1] selects the last element, S[-2] the second last, and so on.
 • Multiple elements of a list or set S can be extracted. The selection op(i..j,S) selects the sub-sequence (S[i],S[i+1],...,S[j]). If S is a set then the selection S[i..j] selects the subset {op(i..j,S)}. If S is a list then the selection S[i..j] selects the sublist [op(i..j,S)]. Negative subscripts can also be used.  Thus S[2..-2] selects all elements except the first and last.
 • Lists and sets can be nested, in which case selection can be done in one of two ways:  S[i][j]...[n] or S[i,j,...,n].
 • To extract the contents of a list or set L as a sequence, use the op(L) function or the empty selection operator L[].

Modifying the elements

Note:  The following modifications to a list or set cause a new list or set to be created rather than modifying the original in-place. To save space and time, when the number of data or the number of modifications to them are large, use of an Array may be preferable. For more information on mutable and immutable data structures, refer to the Basic Data Structures chapter of the Maple Programming Guide.

 • Appending an element x to a list L is done by [op(L), x]. Inserting an element x to a set S is done using the union operator S union {x} ($S\cup \left\{x\right\}$).
 • Replacing the i-th element of a list L by x can be done by subsop(i=x, L).
 • The operation L[i] := x also works for reasons of convenience, but only for short lists. It is not recommended to use this operation unless you know that the list is small.
 • Deleting the i-th element of a list L is subsop(i=NULL, L). Deleting an element x from a set S is done using the minus operator S minus {x} ($S\setminus \left\{x\right\}$).

Set and list operations

 • To test if an element x is in a list or set L, use either member(x,L) or x in L ($x\in L$).
 • To test if a set S is a subset of a set T, use the subset operator  S subset T  ($S\subseteq T$).
 • To find the intersection of two sets S and T, use S intersect T ($S\cap T$).
 • To find the union of two sets S and T, use S union T ($S\cup T$).
 • To find the difference of two sets S and T, use S minus T ($S\setminus T$).
 • For more information on these set operations, see Set Operators.
 • Further list operations can be found in the package ListTools.

Set ordering

Sets have a deterministic ordering that, for most objects, is not based on runtime properties. This means that when {b,c,a} is input, the order will be fixed to {a,b,c} no matter when you created that set. A notable exception to this rule is when a set contains multiple mutable objects of the same type. For example, two vectors inside a set could appear in either order in different sessions.

In general, object ordering is first determined by quickly identifiable features like its class or size.  The default comparison order checks properties in the following order:

 1 object id: this causes the same kind of data-structures to be grouped together.  For example integers will come before exact rationals, floats, and complex numbers.
 2 object length: this causes objects of the same size to be grouped together.  The "length" is a property of the underlying data-structure. As a result, {-2^200, -2^100} is not in increasing numerical order and {bb, aaaaaaaa} is not in alphabetical order; in both cases the shorter data structure occurs first.
 3 lexicographical or numerical order: this orders numbers in increasing numerical order and strings in lexicographic order.
 4 recurse on components: this causes compound objects to be ordered according to the above properties of their child components.  For example {f(z),g(a)} will pass the above properties as both f(z) and g(a) are function objects of the same length.  So the next level of ordering will compare the names of each function, f and g, which will fall into rule 3.  Note that the ordering of expressions like {x+1,1+y} may still be session dependent if the unique representation of x+1 is in the order of 1+x, or if the second expression is y+1 instead of 1+y.
 5 address: in some cases the object will be identical in every way, such as the local name x and the global name x.  When this happens the machine address of the given object will be used.  This at least ensures the set order will be consistent within the running session.

Note:  This order could be changed in future versions of Maple, and/or objects could be represented in a different way in future versions. You should not assume that sets will be maintained in any particular order.

The object-address based set ordering used in Maple 11 and earlier versions can be obtained by using the commandline option --setsort=0. Similarly, the new set order can be enabled with --setsort=1. Variations of --setsort=0..8 are available if you want to purposefully introduce ordering differences into your code to test its robustness.  Settings 2 through 8 apply the same rules 1-5 listed above but alter whether each sub-ordering is increasing or decreasing.  For more information on commandline options, see the maple help page.

 • The [] and {} constructors are thread safe as of Maple 15.

Examples

Sets

Repeated elements in a set are ignored.

 > {x,y,y};
 $\left\{{x}{,}{y}\right\}$ (1)
 > {y,x,y};
 $\left\{{x}{,}{y}\right\}$ (2)

Sets of single letter variables are always sorted in alphabetical order.

 > {B,b,c,a,A};
 $\left\{{A}{,}{B}{,}{a}{,}{b}{,}{c}\right\}$ (3)

Sets of small integers are always in numerical order.

 > S:={5,4,3,2,1};
 ${S}{≔}\left\{{1}{,}{2}{,}{3}{,}{4}{,}{5}\right\}$ (4)
 > $S\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∪\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left\{3,6\right\}$
 $\left\{{1}{,}{2}{,}{3}{,}{4}{,}{5}{,}{6}\right\}$ (5)

Set operators do not change the set.  They create a new set.

 > $S$
 $\left\{{1}{,}{2}{,}{3}{,}{4}{,}{5}\right\}$ (6)

 Set operators do not work on lists.
 > $\left\{1,2,3\right\}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∩\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left\{2,3,4\right\}$
 $\left\{{2}{,}{3}\right\}$ (7)
 > $\left[1,2,3\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∩\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left\{2,3,4\right\}$

The in operator ($\in$) tests for set or list membership.

 > $\mathrm{evalb}\left(1\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∈\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left\{1,2,3\right\}\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{evalb}\left(4\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∈\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[1,2,3\right]\right)$
 ${\mathrm{false}}$ (9)
 > $S≔\left\{1,2\right\}$
 ${S}{≔}\left\{{1}{,}{2}\right\}$ (10)
 > $T≔\left\{1,2,3\right\}$
 ${T}{≔}\left\{{1}{,}{2}{,}{3}\right\}$ (11)

Determine if S is a subset of T:

 > $S⊆T$
 ${\mathrm{true}}$ (12)

Difference between S and T is given by:

 > $T∖S$
 $\left\{{3}\right\}$ (13)

Lists

Elements can be repeated inside a list.

 > $\left[x,y,y\right]$
 $\left[{x}{,}{y}{,}{y}\right]$ (14)
 > $\left[y,x,y\right]$
 $\left[{y}{,}{x}{,}{y}\right]$ (15)

Create a list using the seq command.

 > $L≔\left[\mathrm{seq}\left({x}_{i},i=1..4\right)\right]$
 ${L}{≔}\left[{{x}}_{{1}}{,}{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}\right]$ (16)

Return the number of elements as follows:

 > $\mathrm{numelems}\left(L\right)$
 ${4}$ (17)

Extract specific elements from a list:

 > ${L}_{2}$
 ${{x}}_{{2}}$ (18)
 > ${L}_{1..2}$
 $\left[{{x}}_{{1}}{,}{{x}}_{{2}}\right]$ (19)
 > ${L}_{2..-1}$
 $\left[{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}\right]$ (20)

Extract the contents of a list as a sequence:

 > $\mathrm{op}\left(L\right)$
 ${{x}}_{{1}}{,}{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}$ (21)
 > ${L}_{[]}$
 ${{x}}_{{1}}{,}{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}$ (22)
 > $\mathrm{op}\left({L}_{2..3}\right)$
 ${{x}}_{{2}}{,}{{x}}_{{3}}$ (23)
 > $\mathrm{op}\left(2..3,L\right)$
 ${{x}}_{{2}}{,}{{x}}_{{3}}$ (24)

Append a new element to a list:

 > $L≔\left[\mathrm{op}\left(L\right),{x}_{5}\right]$
 ${L}{≔}\left[{{x}}_{{1}}{,}{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}{,}{{x}}_{{5}}\right]$ (25)

Delete an element from a list:

 > $\mathrm{subsop}\left(2=\mathrm{NULL},L\right)$
 $\left[{{x}}_{{1}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}{,}{{x}}_{{5}}\right]$ (26)

Test for list membership

 > $\mathrm{member}\left({x}_{1},L\right)$
 ${\mathrm{true}}$ (27)

Nested lists:

 > $L≔\left[1,\left[2,3\right],\left[4,\left[5,6\right],7\right],8,9\right]$
 ${L}{≔}\left[{1}{,}\left[{2}{,}{3}\right]{,}\left[{4}{,}\left[{5}{,}{6}\right]{,}{7}\right]{,}{8}{,}{9}\right]$ (28)

Elements from a nested list can be extracted using one of the following methods:

 > ${L}_{3,2,1}$
 ${5}$ (29)
 > ${{{L}_{3}}_{2}}_{1}$
 ${5}$ (30)