Next / Previous / Contents / TCC Help System / NM Tech homepage

7. The sequence types

The next four types described (str, unicode, list and tuple) are collectively referred to as sequence types.

All of them represent an ordered set in the mathematical sense, that is, a collection of things in a specific order.

7.1. Intrinsic functions common to all sequences

All four of the basic sequence types (str, unicode, list, and tuple) share a common set of functions. In the table below, S means any sequence:

len(S)The number of elements in S.
max(S)The largest element of S.
min(S)The smallest element of S.
x in STrue if any members of S are equal to x. A predicate function, like the “>=” and other comparison operators..
x not in STrue if none of the elements of S are equal to x.
S1+S2Concatenate two sequences.
tuple(S)Convert the members of S to a tuple.
list(S)Convert S to a list.
S*nA new sequence containing n copies of the contents of S. For example, [0]*5 is a list of five zeroes.
S[i]Subscripting: Refers to element i of sequence S, counting from zero. For example, "abcd"[2] is "c". A negative index value is counted from the end back toward the front. Element -1 refers to the last element, -2 to the next-to-last, and so on, so "abcdef"[-1] is "f".
S[i:j]Retrieve another sequence that is a slice of sequence S starting at element i (counting from zero) and going up to but not including element j. For example, "abcdef"[2:4] is "cd". You can omit i to start the slice at the beginning of s; you can omit j to take a slice all the way to the end of s. For example, "abcdef"[:3] is "abc", and "abcdef"[3:] is "def". See the diagram below. You can even omit both parts; S[:] gives you a new sequence that is a copy of the old one.

In the diagram above, if string s is "abcdef", slice s[2:5] is "cde", slice s[:3] is "abc", and s[3:] is "def".