@@ -958,8 +958,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
958
958
pair: slice; operation
959
959
pair: operator; in
960
960
pair: operator; not in
961
- single: count() (sequence method)
962
- single: index() (sequence method)
963
961
964
962
+--------------------------+--------------------------------+----------+
965
963
| Operation | Result | Notes |
@@ -976,7 +974,7 @@ operations have the same priority as the corresponding numeric operations. [3]_
976
974
| ``s * n `` or | equivalent to adding *s * to | (2)(7) |
977
975
| ``n * s `` | itself *n * times | |
978
976
+--------------------------+--------------------------------+----------+
979
- | ``s[i] `` | *i *\ th item of *s *, origin 0 | (3)(9 ) |
977
+ | ``s[i] `` | *i *\ th item of *s *, origin 0 | (3)(8 ) |
980
978
+--------------------------+--------------------------------+----------+
981
979
| ``s[i:j] `` | slice of *s * from *i * to *j * | (3)(4) |
982
980
+--------------------------+--------------------------------+----------+
@@ -989,13 +987,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
989
987
+--------------------------+--------------------------------+----------+
990
988
| ``max(s) `` | largest item of *s * | |
991
989
+--------------------------+--------------------------------+----------+
992
- | ``s.index(x[, i[, j]]) `` | index of the first occurrence | \( 8) |
993
- | | of *x * in *s * (at or after | |
994
- | | index *i * and before index *j *)| |
995
- +--------------------------+--------------------------------+----------+
996
- | ``s.count(x) `` | total number of occurrences of | |
997
- | | *x * in *s * | |
998
- +--------------------------+--------------------------------+----------+
999
990
1000
991
Sequences of the same type also support comparisons. In particular, tuples
1001
992
and lists are compared lexicographically by comparing corresponding elements.
@@ -1101,16 +1092,42 @@ Notes:
1101
1092
concatenation or repetition.
1102
1093
1103
1094
(8)
1104
- ``index `` raises :exc: `ValueError ` when *x * is not found in *s *.
1105
- Not all implementations support passing the additional arguments *i * and *j *.
1106
- These arguments allow efficient searching of subsections of the sequence. Passing
1107
- the extra arguments is roughly equivalent to using ``s[i:j].index(x) ``, only
1108
- without copying any data and with the returned index being relative to
1109
- the start of the sequence rather than the start of the slice.
1110
-
1111
- (9)
1112
1095
An :exc: `IndexError ` is raised if *i * is outside the sequence range.
1113
1096
1097
+ .. rubric :: Sequence Methods
1098
+
1099
+ Sequence types also support the following methods:
1100
+
1101
+ .. method :: list.count(value, /)
1102
+ range.count(value, /)
1103
+ tuple.count(value, /)
1104
+ :no-contents-entry:
1105
+ :no-index-entry:
1106
+ :no-typesetting:
1107
+ .. method :: sequence.count(value, /)
1108
+
1109
+ Return the total number of occurrences of *value * in *sequence *.
1110
+
1111
+ .. method :: list.index(value[, start[, stop])
1112
+ range.index(value[, start[, stop])
1113
+ tuple.index(value[, start[, stop])
1114
+ :no-contents-entry:
1115
+ :no-index-entry:
1116
+ :no-typesetting:
1117
+ .. method :: sequence.index(value[, start[, stop])
1118
+
1119
+ Return the index of the first occurrence of *value * in *sequence *.
1120
+
1121
+ Raises :exc: `ValueError ` if *value * is not found in *sequence *.
1122
+
1123
+ The *start * or *stop * arguments allow for efficient searching
1124
+ of subsections of the sequence, beginning at *start * and ending at *stop *.
1125
+ This is roughly equivalent to ``start + sequence[start:stop].index(value) ``,
1126
+ only without copying any data.
1127
+
1128
+ .. caution ::
1129
+ Not all sequence types support passing the *start * and *stop * arguments.
1130
+
1114
1131
1115
1132
.. _typesseq-immutable :
1116
1133
@@ -1160,14 +1177,6 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
1160
1177
pair: subscript; assignment
1161
1178
pair: slice; assignment
1162
1179
pair: statement; del
1163
- single: append() (sequence method)
1164
- single: clear() (sequence method)
1165
- single: copy() (sequence method)
1166
- single: extend() (sequence method)
1167
- single: insert() (sequence method)
1168
- single: pop() (sequence method)
1169
- single: remove() (sequence method)
1170
- single: reverse() (sequence method)
1171
1180
1172
1181
+------------------------------+--------------------------------+---------------------+
1173
1182
| Operation | Result | Notes |
@@ -1191,72 +1200,120 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
1191
1200
| ``del s[i:j:k] `` | removes the elements of | |
1192
1201
| | ``s[i:j:k] `` from the list | |
1193
1202
+------------------------------+--------------------------------+---------------------+
1194
- | ``s.append(x) `` | appends *x * to the end of the | |
1195
- | | sequence (same as | |
1196
- | | ``s[len(s):len(s)] = [x] ``) | |
1197
- +------------------------------+--------------------------------+---------------------+
1198
- | ``s.clear() `` | removes all items from *s * | \( 5) |
1199
- | | (same as ``del s[:] ``) | |
1200
- +------------------------------+--------------------------------+---------------------+
1201
- | ``s.copy() `` | creates a shallow copy of *s * | \( 5) |
1202
- | | (same as ``s[:] ``) | |
1203
- +------------------------------+--------------------------------+---------------------+
1204
- | ``s.extend(t) `` or | extends *s * with the | |
1205
- | ``s += t `` | contents of *t * (for the | |
1203
+ | ``s += t `` | extends *s * with the | |
1204
+ | | contents of *t * (for the | |
1206
1205
| | most part the same as | |
1207
1206
| | ``s[len(s):len(s)] = t ``) | |
1208
1207
+------------------------------+--------------------------------+---------------------+
1209
- | ``s *= n `` | updates *s * with its contents | \( 6 ) |
1208
+ | ``s *= n `` | updates *s * with its contents | \( 2 ) |
1210
1209
| | repeated *n * times | |
1211
1210
+------------------------------+--------------------------------+---------------------+
1212
- | ``s.insert(i, x) `` | inserts *x * into *s * at the | |
1213
- | | index given by *i * | |
1214
- | | (same as ``s[i:i] = [x] ``) | |
1215
- +------------------------------+--------------------------------+---------------------+
1216
- | ``s.pop() `` or ``s.pop(i) `` | retrieves the item at *i * and | \( 2) |
1217
- | | also removes it from *s * | |
1218
- +------------------------------+--------------------------------+---------------------+
1219
- | ``s.remove(x) `` | removes the first item from | \( 3) |
1220
- | | *s * where ``s[i] `` is equal to | |
1221
- | | *x * | |
1222
- +------------------------------+--------------------------------+---------------------+
1223
- | ``s.reverse() `` | reverses the items of *s * in | \( 4) |
1224
- | | place | |
1225
- +------------------------------+--------------------------------+---------------------+
1226
-
1227
1211
1228
1212
Notes:
1229
1213
1230
1214
(1)
1231
1215
If *k * is not equal to ``1 ``, *t * must have the same length as the slice it is replacing.
1232
1216
1233
1217
(2)
1234
- The optional argument *i * defaults to ``-1 ``, so that by default the last
1235
- item is removed and returned.
1218
+ The value *n * is an integer, or an object implementing
1219
+ :meth: `~object.__index__ `. Zero and negative values of *n * clear
1220
+ the sequence. Items in the sequence are not copied; they are referenced
1221
+ multiple times, as explained for ``s * n `` under :ref: `typesseq-common `.
1236
1222
1237
- (3)
1238
- :meth: `remove ` raises :exc: `ValueError ` when *x * is not found in *s *.
1223
+ .. rubric :: Mutable Sequence Methods
1239
1224
1240
- (4)
1241
- The :meth: `reverse ` method modifies the sequence in place for economy of
1242
- space when reversing a large sequence. To remind users that it operates by
1243
- side effect, it does not return the reversed sequence.
1225
+ Mutable sequence types also support the following methods:
1244
1226
1245
- (5)
1246
- :meth: `clear ` and :meth: `!copy ` are included for consistency with the
1247
- interfaces of mutable containers that don't support slicing operations
1248
- (such as :class: `dict ` and :class: `set `). :meth: `!copy ` is not part of the
1249
- :class: `collections.abc.MutableSequence ` ABC, but most concrete
1250
- mutable sequence classes provide it.
1227
+ .. method :: bytearray.append(value, /)
1228
+ list.append(value, /)
1229
+ :no-contents-entry:
1230
+ :no-index-entry:
1231
+ :no-typesetting:
1232
+ .. method :: sequence.append(value, /)
1233
+
1234
+ Append *value * to the end of the sequence
1235
+ This is equivalent to writing ``seq[len(seq):len(seq)] = [value] ``.
1236
+
1237
+ .. method :: bytearray.clear()
1238
+ list.clear()
1239
+ :no-contents-entry:
1240
+ :no-index-entry:
1241
+ :no-typesetting:
1242
+ .. method :: sequence.clear()
1251
1243
1252
1244
.. versionadded :: 3.3
1253
- :meth: `clear ` and :meth: `!copy ` methods.
1254
1245
1255
- (6)
1256
- The value *n * is an integer, or an object implementing
1257
- :meth: `~object.__index__ `. Zero and negative values of *n * clear
1258
- the sequence. Items in the sequence are not copied; they are referenced
1259
- multiple times, as explained for ``s * n `` under :ref: `typesseq-common `.
1246
+ Remove all items from *sequence *.
1247
+ This is equivalent to writing ``del sequence[:] ``.
1248
+
1249
+ .. method :: bytearray.copy()
1250
+ list.copy()
1251
+ :no-contents-entry:
1252
+ :no-index-entry:
1253
+ :no-typesetting:
1254
+ .. method :: sequence.copy()
1255
+
1256
+ .. versionadded :: 3.3
1257
+
1258
+ Create a shallow copy of *sequence *.
1259
+ This is equivalent to writing ``sequence[:] ``.
1260
+
1261
+ .. hint :: The :meth:`!copy` method is not part of the
1262
+ :class: `~collections.abc.MutableSequence ` :class: `~abc.ABC `,
1263
+ but most concrete mutable sequence types provide it.
1264
+
1265
+ .. method :: bytearray.extend(iterable, /)
1266
+ list.extend(iterable, /)
1267
+ :no-contents-entry:
1268
+ :no-index-entry:
1269
+ :no-typesetting:
1270
+ .. method :: sequence.extend(iterable, /)
1271
+
1272
+ Extend *sequence * with the contents of *iterable *.
1273
+ For the most part, this is the same as writing
1274
+ ``seq[len(seq):len(seq)] = iterable ``.
1275
+
1276
+ .. method :: bytearray.insert(index, value, /)
1277
+ list.insert(index, value, /)
1278
+ :no-contents-entry:
1279
+ :no-index-entry:
1280
+ :no-typesetting:
1281
+ .. method :: sequence.insert(index, value, /)
1282
+
1283
+ Insert *value * into *sequence * at the given *index *.
1284
+ This is equivalent to writing ``sequence[index:index] = [value] ``.
1285
+
1286
+ .. method :: bytearray.pop(index=-1, /)
1287
+ list.pop(index=-1, /)
1288
+ :no-contents-entry:
1289
+ :no-index-entry:
1290
+ :no-typesetting:
1291
+ .. method :: sequence.pop(index=-1, /)
1292
+
1293
+ Retrieve the item at *index * and also removes it from *sequence *.
1294
+ By default, the last item in *sequence * is removed and returned.
1295
+
1296
+ .. method :: bytearray.remove(value, /)
1297
+ list.remove(value, /)
1298
+ :no-contents-entry:
1299
+ :no-index-entry:
1300
+ :no-typesetting:
1301
+ .. method :: sequence.remove(value, /)
1302
+
1303
+ Remove the first item from *sequence * where ``sequence[i] == value ``.
1304
+
1305
+ Raises :exc: `ValueError ` if *value * is not found in *sequence *.
1306
+
1307
+ .. method :: bytearray.reverse()
1308
+ list.reverse()
1309
+ :no-contents-entry:
1310
+ :no-index-entry:
1311
+ :no-typesetting:
1312
+ .. method :: sequence.reverse()
1313
+
1314
+ Reverse the items of *sequence * in place.
1315
+ This method maintains economy of space when reversing a large sequence.
1316
+ To remind users that it operates by side-effect, it returns ``None ``.
1260
1317
1261
1318
1262
1319
.. _typesseq-list :
@@ -5562,9 +5619,10 @@ Methods
5562
5619
5563
5620
.. index :: pair: object; method
5564
5621
5565
- Methods are functions that are called using the attribute notation. There are
5566
- two flavors: :ref: `built-in methods <builtin-methods >` (such as :meth: `append `
5567
- on lists) and :ref: `class instance method <instance-methods >`.
5622
+ Methods are functions that are called using the attribute notation.
5623
+ There are two flavors: :ref: `built-in methods <builtin-methods >`
5624
+ (such as :meth: `~list.append ` on lists)
5625
+ and :ref: `class instance method <instance-methods >`.
5568
5626
Built-in methods are described with the types that support them.
5569
5627
5570
5628
If you access a method (a function defined in a class namespace) through an
0 commit comments