Skip to Main Content
Python Programming On Win32
book

Python Programming On Win32

by Andy Robinson, Mark Hammond
January 2000
Intermediate to advanced content levelIntermediate to advanced
672 pages
21h 46m
English
O'Reilly Media, Inc.
Content preview from Python Programming On Win32

Mutable Sequence Types

List objects support additional operations that allow in-place modification of the object. These operations would be supported by other mutable sequence types (when added to the language) as well. Strings and tuples are immutable sequence types, and such objects can’t be modified once created. The operations in the following table are defined on mutable sequence types (where x is an arbitrary object).

Operation

Result

Notes

s[i] = x

Item i of s is replaced by x

 

s[i:j] = t

Slice of s from i to j is replaced by t

 

del s[i:j]

Same as s[i:j] = []

 

s.append(x)

Same as s[len(s):len(s)] = [x]

 

s.extend(x)

Same as s[len(s):len(s)] = x

5

s.count(x)

Return number of i’s for which s[i] == x

 

s.index(x)

Return smallest i such that s[i] == x

1

s.insert(i, x)

Same as s[i:i] = [x] if i >= 0

 

s.pop([i])

Same as x = s[i]; del s[i]; return x

4

s.remove(x)

Same as del s[s.index(x)]

1

s.reverse()

Reverses the items of s in place

3

s.sort([cmpfunc])

Sort the items of s in place

2, 3

Notes

  1. This raises an exception when x is not found in s.

  2. The sort() method takes an optional argument specifying a comparison function of two arguments (list items) that should return -1, 0, or 1 depending on whether the first argument is considered smaller than, equal to, or larger than the second argument. Note that this slows the sorting process considerably; e.g., to sort a list in reverse order, it’s much faster to use calls to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Programming - Second Edition

Advanced Python Programming - Second Edition

Quan Nguyen

Publisher Resources

ISBN: 1565926218Supplemental ContentErrata Page