Programming Search⌘K

Featurecore

Sequence types

Also called sequence, common sequence operations, list tuple range, mutable sequence, immutable sequence, in operator on sequences, sequence repetition.

list, tuple and range are the basic sequence types. They share operations such as in, +, *, indexing, slicing, len, min, max, count and index. Mutable sequences also support item and slice assignment.

Language
Python

Synopsis

for s in ([3, 1, 2], (3, 1, 2), range(1, 4), "312"):
    print(type(s).__name__, len(s), min(s), max(s), s[0], s.index(s[-1]))
Output
list 3 1 3 3 2
tuple 3 1 3 3 2
range 3 1 3 1 2
str 3 1 3 3 2

The same operations work on a list, a tuple, a range and a string.

Semantics

r1. "There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections."1

r2. "The operations in the following table are supported by most sequence types, both mutable and immutable."1 They are x in s, x not in s, s + t, s * n, s[i], s[i:j], s[i:j:k], len(s), min(s), max(s), and the methods count and index. Example: operations.

r3. "Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s). Note that items in the sequence s are not copied; they are referenced multiple times."1 Example: repeat-lists.

r4. "If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0."1 "An IndexError is raised if i is outside the sequence range."1 Example: out-of-range.

r5. "The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the hash() built-in."1 "This support allows immutable sequences, such as tuple instances, to be used as dict keys and stored in set and frozenset instances."1

r6. Mutable sequence types also support s[i] = x, del s[i], s[i:j] = t, del s[i:j], s += t and s *= n.1 Example: mutable.

r7. "Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length."1

Examples

s = [10, 20, 30, 20]
t = (1, 2)
print(s + [40], t * 2)
print(s[-1], s[1:3], s[::2])
print(s.count(20), s.index(20), s.index(20, 2))
print(30 not in s, "gg" in "eggs")
Output
[10, 20, 30, 20, 40] (1, 2, 1, 2)
20 [20, 30] [10, 30]
2 1 3
False True

For str, in also tests for a substring, so "gg" in "eggs" is true.

nums = [1, 2, 3, 4, 5]
nums[0] = 100
del nums[1]
nums[1:3] = ["a", "b", "c"]
nums += [6]
print(nums)
Output
[100, 'a', 'b', 'c', 5, 6]

A slice assignment can change the length of the list (rule r6).

point = (2, 3)
seen = {point: "start"}
print(seen[(2, 3)], hash((1, 2)) == hash((1, 2)))
Output
start True

A tuple of hashable items can be a dict key (rule r5).

Pitfalls

Repetition copies references, not items:

This code runs but gives a wrong answer on purpose.

rows = [[]] * 3
rows[0].append(3)
print(rows)
Output
[[3], [3], [3]]

The three items are one list (rule r3). Use [[] for i in range(3)] to get three lists.

An index past the end is an error:

This code raises IndexError, on purpose.

letters = ["a", "b", "c"]
letters[3]

Comparison

Go has arrays and slices. "An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the array and is never negative."3 A Go array has a fixed length that is part of its type. A Python list, tuple or range can have any length, and all three share the operations of rule r2.

References

  1. Built-in Types, Python 3.15 documentation, Sequence Types; Common Sequence Operations; Immutable Sequence Types; Mutable Sequence Types
  2. The Python Tutorial, 5. Data Structures, 5.3. Tuples and Sequences
  3. The Go Programming Language Specification, Array types; Slice types

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source