@@ -1000,8 +1000,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
1000
1000
pair: slice; operation
1001
1001
pair: operator; in
1002
1002
pair: operator; not in
1003
- single: count() (sequence method)
1004
- single: index() (sequence method)
1005
1003
1006
1004
+--------------------------+--------------------------------+----------+
1007
1005
| Operation | Result | Notes |
@@ -1018,7 +1016,7 @@ operations have the same priority as the corresponding numeric operations. [3]_
1018
1016
| ``s * n `` or | equivalent to adding *s * to | (2)(7) |
1019
1017
| ``n * s `` | itself *n * times | |
1020
1018
+--------------------------+--------------------------------+----------+
1021
- | ``s[i] `` | *i *\ th item of *s *, origin 0 | (3)(9 ) |
1019
+ | ``s[i] `` | *i *\ th item of *s *, origin 0 | (3)(8 ) |
1022
1020
+--------------------------+--------------------------------+----------+
1023
1021
| ``s[i:j] `` | slice of *s * from *i * to *j * | (3)(4) |
1024
1022
+--------------------------+--------------------------------+----------+
@@ -1031,13 +1029,6 @@ operations have the same priority as the corresponding numeric operations. [3]_
1031
1029
+--------------------------+--------------------------------+----------+
1032
1030
| ``max(s) `` | largest item of *s * | |
1033
1031
+--------------------------+--------------------------------+----------+
1034
- | ``s.index(x[, i[, j]]) `` | index of the first occurrence | \( 8) |
1035
- | | of *x * in *s * (at or after | |
1036
- | | index *i * and before index *j *)| |
1037
- +--------------------------+--------------------------------+----------+
1038
- | ``s.count(x) `` | total number of occurrences of | |
1039
- | | *x * in *s * | |
1040
- +--------------------------+--------------------------------+----------+
1041
1032
1042
1033
Sequences of the same type also support comparisons. In particular, tuples
1043
1034
and lists are compared lexicographically by comparing corresponding elements.
@@ -1143,16 +1134,42 @@ Notes:
1143
1134
concatenation or repetition.
1144
1135
1145
1136
(8)
1146
- ``index `` raises :exc: `ValueError ` when *x * is not found in *s *.
1147
- Not all implementations support passing the additional arguments *i * and *j *.
1148
- These arguments allow efficient searching of subsections of the sequence. Passing
1149
- the extra arguments is roughly equivalent to using ``s[i:j].index(x) ``, only
1150
- without copying any data and with the returned index being relative to
1151
- the start of the sequence rather than the start of the slice.
1152
-
1153
- (9)
1154
1137
An :exc: `IndexError ` is raised if *i * is outside the sequence range.
1155
1138
1139
+ .. rubric :: Sequence Methods
1140
+
1141
+ Sequence types also support the following methods:
1142
+
1143
+ .. method :: list.count(value, /)
1144
+ range.count(value, /)
1145
+ tuple.count(value, /)
1146
+ :no-contents-entry:
1147
+ :no-index-entry:
1148
+ :no-typesetting:
1149
+ .. method :: sequence.count(value, /)
1150
+
1151
+ Return the total number of occurrences of *value * in *sequence *.
1152
+
1153
+ .. method :: list.index(value[, start[, stop])
1154
+ range.index(value[, start[, stop])
1155
+ tuple.index(value[, start[, stop])
1156
+ :no-contents-entry:
1157
+ :no-index-entry:
1158
+ :no-typesetting:
1159
+ .. method :: sequence.index(value[, start[, stop])
1160
+
1161
+ Return the index of the first occurrence of *value * in *sequence *.
1162
+
1163
+ Raises :exc: `ValueError ` if *value * is not found in *sequence *.
1164
+
1165
+ The *start * or *stop * arguments allow for efficient searching
1166
+ of subsections of the sequence, beginning at *start * and ending at *stop *.
1167
+ This is roughly equivalent to ``start + sequence[start:stop].index(value) ``,
1168
+ only without copying any data.
1169
+
1170
+ .. caution ::
1171
+ Not all sequence types support passing the *start * and *stop * arguments.
1172
+
1156
1173
1157
1174
.. _typesseq-immutable :
1158
1175
@@ -1202,14 +1219,6 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
1202
1219
pair: subscript; assignment
1203
1220
pair: slice; assignment
1204
1221
pair: statement; del
1205
- single: append() (sequence method)
1206
- single: clear() (sequence method)
1207
- single: copy() (sequence method)
1208
- single: extend() (sequence method)
1209
- single: insert() (sequence method)
1210
- single: pop() (sequence method)
1211
- single: remove() (sequence method)
1212
- single: reverse() (sequence method)
1213
1222
1214
1223
+------------------------------+--------------------------------+---------------------+
1215
1224
| Operation | Result | Notes |
@@ -1233,72 +1242,120 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
1233
1242
| ``del s[i:j:k] `` | removes the elements of | |
1234
1243
| | ``s[i:j:k] `` from the list | |
1235
1244
+------------------------------+--------------------------------+---------------------+
1236
- | ``s.append(x) `` | appends *x * to the end of the | |
1237
- | | sequence (same as | |
1238
- | | ``s[len(s):len(s)] = [x] ``) | |
1239
- +------------------------------+--------------------------------+---------------------+
1240
- | ``s.clear() `` | removes all items from *s * | \( 5) |
1241
- | | (same as ``del s[:] ``) | |
1242
- +------------------------------+--------------------------------+---------------------+
1243
- | ``s.copy() `` | creates a shallow copy of *s * | \( 5) |
1244
- | | (same as ``s[:] ``) | |
1245
- +------------------------------+--------------------------------+---------------------+
1246
- | ``s.extend(t) `` or | extends *s * with the | |
1247
- | ``s += t `` | contents of *t * (for the | |
1245
+ | ``s += t `` | extends *s * with the | |
1246
+ | | contents of *t * (for the | |
1248
1247
| | most part the same as | |
1249
1248
| | ``s[len(s):len(s)] = t ``) | |
1250
1249
+------------------------------+--------------------------------+---------------------+
1251
- | ``s *= n `` | updates *s * with its contents | \( 6 ) |
1250
+ | ``s *= n `` | updates *s * with its contents | \( 2 ) |
1252
1251
| | repeated *n * times | |
1253
1252
+------------------------------+--------------------------------+---------------------+
1254
- | ``s.insert(i, x) `` | inserts *x * into *s * at the | |
1255
- | | index given by *i * | |
1256
- | | (same as ``s[i:i] = [x] ``) | |
1257
- +------------------------------+--------------------------------+---------------------+
1258
- | ``s.pop() `` or ``s.pop(i) `` | retrieves the item at *i * and | \( 2) |
1259
- | | also removes it from *s * | |
1260
- +------------------------------+--------------------------------+---------------------+
1261
- | ``s.remove(x) `` | removes the first item from | \( 3) |
1262
- | | *s * where ``s[i] `` is equal to | |
1263
- | | *x * | |
1264
- +------------------------------+--------------------------------+---------------------+
1265
- | ``s.reverse() `` | reverses the items of *s * in | \( 4) |
1266
- | | place | |
1267
- +------------------------------+--------------------------------+---------------------+
1268
-
1269
1253
1270
1254
Notes:
1271
1255
1272
1256
(1)
1273
1257
If *k * is not equal to ``1 ``, *t * must have the same length as the slice it is replacing.
1274
1258
1275
1259
(2)
1276
- The optional argument *i * defaults to ``-1 ``, so that by default the last
1277
- item is removed and returned.
1260
+ The value *n * is an integer, or an object implementing
1261
+ :meth: `~object.__index__ `. Zero and negative values of *n * clear
1262
+ the sequence. Items in the sequence are not copied; they are referenced
1263
+ multiple times, as explained for ``s * n `` under :ref: `typesseq-common `.
1278
1264
1279
- (3)
1280
- :meth: `remove ` raises :exc: `ValueError ` when *x * is not found in *s *.
1265
+ .. rubric :: Mutable Sequence Methods
1281
1266
1282
- (4)
1283
- The :meth: `reverse ` method modifies the sequence in place for economy of
1284
- space when reversing a large sequence. To remind users that it operates by
1285
- side effect, it does not return the reversed sequence.
1267
+ Mutable sequence types also support the following methods:
1286
1268
1287
- (5)
1288
- :meth: `clear ` and :meth: `!copy ` are included for consistency with the
1289
- interfaces of mutable containers that don't support slicing operations
1290
- (such as :class: `dict ` and :class: `set `). :meth: `!copy ` is not part of the
1291
- :class: `collections.abc.MutableSequence ` ABC, but most concrete
1292
- mutable sequence classes provide it.
1269
+ .. method :: bytearray.append(value, /)
1270
+ list.append(value, /)
1271
+ :no-contents-entry:
1272
+ :no-index-entry:
1273
+ :no-typesetting:
1274
+ .. method :: sequence.append(value, /)
1275
+
1276
+ Append *value * to the end of the sequence
1277
+ This is equivalent to writing ``seq[len(seq):len(seq)] = [value] ``.
1278
+
1279
+ .. method :: bytearray.clear()
1280
+ list.clear()
1281
+ :no-contents-entry:
1282
+ :no-index-entry:
1283
+ :no-typesetting:
1284
+ .. method :: sequence.clear()
1293
1285
1294
1286
.. versionadded :: 3.3
1295
- :meth: `clear ` and :meth: `!copy ` methods.
1296
1287
1297
- (6)
1298
- The value *n * is an integer, or an object implementing
1299
- :meth: `~object.__index__ `. Zero and negative values of *n * clear
1300
- the sequence. Items in the sequence are not copied; they are referenced
1301
- multiple times, as explained for ``s * n `` under :ref: `typesseq-common `.
1288
+ Remove all items from *sequence *.
1289
+ This is equivalent to writing ``del sequence[:] ``.
1290
+
1291
+ .. method :: bytearray.copy()
1292
+ list.copy()
1293
+ :no-contents-entry:
1294
+ :no-index-entry:
1295
+ :no-typesetting:
1296
+ .. method :: sequence.copy()
1297
+
1298
+ .. versionadded :: 3.3
1299
+
1300
+ Create a shallow copy of *sequence *.
1301
+ This is equivalent to writing ``sequence[:] ``.
1302
+
1303
+ .. hint :: The :meth:`!copy` method is not part of the
1304
+ :class: `~collections.abc.MutableSequence ` :class: `~abc.ABC `,
1305
+ but most concrete mutable sequence types provide it.
1306
+
1307
+ .. method :: bytearray.extend(iterable, /)
1308
+ list.extend(iterable, /)
1309
+ :no-contents-entry:
1310
+ :no-index-entry:
1311
+ :no-typesetting:
1312
+ .. method :: sequence.extend(iterable, /)
1313
+
1314
+ Extend *sequence * with the contents of *iterable *.
1315
+ For the most part, this is the same as writing
1316
+ ``seq[len(seq):len(seq)] = iterable ``.
1317
+
1318
+ .. method :: bytearray.insert(index, value, /)
1319
+ list.insert(index, value, /)
1320
+ :no-contents-entry:
1321
+ :no-index-entry:
1322
+ :no-typesetting:
1323
+ .. method :: sequence.insert(index, value, /)
1324
+
1325
+ Insert *value * into *sequence * at the given *index *.
1326
+ This is equivalent to writing ``sequence[index:index] = [value] ``.
1327
+
1328
+ .. method :: bytearray.pop(index=-1, /)
1329
+ list.pop(index=-1, /)
1330
+ :no-contents-entry:
1331
+ :no-index-entry:
1332
+ :no-typesetting:
1333
+ .. method :: sequence.pop(index=-1, /)
1334
+
1335
+ Retrieve the item at *index * and also removes it from *sequence *.
1336
+ By default, the last item in *sequence * is removed and returned.
1337
+
1338
+ .. method :: bytearray.remove(value, /)
1339
+ list.remove(value, /)
1340
+ :no-contents-entry:
1341
+ :no-index-entry:
1342
+ :no-typesetting:
1343
+ .. method :: sequence.remove(value, /)
1344
+
1345
+ Remove the first item from *sequence * where ``sequence[i] == value ``.
1346
+
1347
+ Raises :exc: `ValueError ` if *value * is not found in *sequence *.
1348
+
1349
+ .. method :: bytearray.reverse()
1350
+ list.reverse()
1351
+ :no-contents-entry:
1352
+ :no-index-entry:
1353
+ :no-typesetting:
1354
+ .. method :: sequence.reverse()
1355
+
1356
+ Reverse the items of *sequence * in place.
1357
+ This method maintains economy of space when reversing a large sequence.
1358
+ To remind users that it operates by side-effect, it returns ``None ``.
1302
1359
1303
1360
1304
1361
.. _typesseq-list :
@@ -5761,9 +5818,10 @@ Methods
5761
5818
5762
5819
.. index :: pair: object; method
5763
5820
5764
- Methods are functions that are called using the attribute notation. There are
5765
- two flavors: :ref: `built-in methods <builtin-methods >` (such as :meth: `append `
5766
- on lists) and :ref: `class instance method <instance-methods >`.
5821
+ Methods are functions that are called using the attribute notation.
5822
+ There are two flavors: :ref: `built-in methods <builtin-methods >`
5823
+ (such as :meth: `~list.append ` on lists)
5824
+ and :ref: `class instance method <instance-methods >`.
5767
5825
Built-in methods are described with the types that support them.
5768
5826
5769
5827
If you access a method (a function defined in a class namespace) through an
0 commit comments