|
| 1 | +============== |
| 2 | +[] (slicing) |
| 3 | +============== |
| 4 | + |
| 5 | +Description |
| 6 | +=========== |
| 7 | +Gives access to a specified range of sequence's elements. |
| 8 | + |
| 9 | +Syntax |
| 10 | +====== |
| 11 | +**sequence** *[start:stop[:step]]* |
| 12 | + |
| 13 | +*start* |
| 14 | + Optional. Starting index of the slice. Defaults to 0. |
| 15 | +*stop* |
| 16 | + Optional. The last index of the slice or the number of items to get. Defaults to *len(sequence)*. |
| 17 | +*step* |
| 18 | + Optional. Extended slice syntax. Step value of the slice. Defaults to 1. |
| 19 | + |
| 20 | +Return Value |
| 21 | +============ |
| 22 | +The same as selected. |
| 23 | + |
| 24 | +Time Complexity |
| 25 | +=============== |
| 26 | +#TODO |
| 27 | + |
| 28 | +Remarks |
| 29 | +======= |
| 30 | +Consider the following ASCII graph showing the contents of the "ABCD" string: |
| 31 | + |
| 32 | +>>> +---+---+---+---+ |
| 33 | +>>> |-4 |-3 |-2 |-1 | <= negative indexes |
| 34 | +>>> +---+---+---+---+ |
| 35 | +>>> | A | B | C | D | <= sequence elements |
| 36 | +>>> +---+---+---+---+ |
| 37 | +>>> | 0 | 1 | 2 | 3 | <= positive indexes |
| 38 | +>>> +---+---+---+---+ |
| 39 | +>>> |<- 0:3:1 ->| <= extent of the slice: "ABCD"[0:3:1] |
| 40 | + |
| 41 | + |
| 42 | +Consider the following example: |
| 43 | + |
| 44 | +Example 1 |
| 45 | +========= |
| 46 | +>>> "ABCD"[0:2] |
| 47 | +'AB' |
| 48 | + |
| 49 | +It can be read as: get every single one item between indexes 0 and 2 (exclusive). |
| 50 | + |
| 51 | +The next example shows usage of the *step* argument: |
| 52 | + |
| 53 | +Example 2 |
| 54 | +========= |
| 55 | +>>> "ABCD"[0:4:2] |
| 56 | +'AC' |
| 57 | + |
| 58 | +That can be interpreted as: get every second element between indexes 0 and 4. |
| 59 | + |
| 60 | +Usage of start, stop and step operators is optional: |
| 61 | + |
| 62 | +Example 3 |
| 63 | +========= |
| 64 | +>>> "ABCD"[1:] |
| 65 | +'BCD' |
| 66 | +>>> "ABCD"[:3] |
| 67 | +'ABC' |
| 68 | +>>> "ABCD"[1:3] |
| 69 | +'BC' |
| 70 | +>>> "ABCD"[1:3:] |
| 71 | +'BC' |
| 72 | +>>> "ABCD"[::2] |
| 73 | +'AC' |
| 74 | +>>> "ABCD"[::] |
| 75 | +'ABCD' |
| 76 | +>>> "ABCD"[:] |
| 77 | +'ABCD' |
| 78 | + |
| 79 | +Negative step argument can be used to reverse the sequence: |
| 80 | + |
| 81 | +Example 4 |
| 82 | +========= |
| 83 | +>>> "ABCD"[::-1] |
| 84 | +'DCBA' |
| 85 | +>>> [0, 1, 2, 3][::-1] |
| 86 | +[3, 2, 1, 0] |
| 87 | + |
| 88 | +Example 5 |
| 89 | +========= |
| 90 | +>>> # slices can be used to replace multiple items |
| 91 | +>>> l = [0, 1, 2, 3] |
| 92 | +>>> l[:2] = ("AB", "CD") |
| 93 | +>>> l |
| 94 | +['AB', 'CD', 2, 3] |
| 95 | + |
| 96 | +Example 6 |
| 97 | +========= |
| 98 | +>>> l = [0, 1, 2, 3] |
| 99 | +>>> l[1:2] = (7, 8, 9, 10) |
| 100 | +>>> l |
| 101 | +[0, 7, 8, 9, 10, 2, 3] |
| 102 | + |
| 103 | +Example 7 |
| 104 | +========= |
| 105 | +>>> # when using extended slice syntax both chunks must match |
| 106 | +>>> l = [0, 1, 2, 3] |
| 107 | +>>> l[::2] = "ABCD" |
| 108 | +Traceback (most recent call last): |
| 109 | + File "<interactive input>", line 1, in <module> |
| 110 | +ValueError: attempt to assign sequence of size 4 to extended slice of size 2 |
| 111 | + |
| 112 | +Example 8 |
| 113 | +========= |
| 114 | +>>> # deleting items |
| 115 | +>>> l = [0, 1, 2, 3] |
| 116 | +>>> del l[::2] |
| 117 | +>>> l |
| 118 | +[1, 3] |
| 119 | + |
| 120 | +See Also |
| 121 | +======== |
| 122 | +#TODO |
0 commit comments