Python list slice usage example,
This example describes how to use Python list slicing. We will share this with you for your reference. The details are as follows:
Slice is supported for ordered sequences in Python, such as lists, strings, and tuples.
Format: [start: end: step]
Start: start index, starting from 0.-1 indicates the end.
End: end Index
Step: step Size, end-start, and the step size is positive. The value ranges from left to right. If the step size is negative, the reverse value is used.
Note that the slice result does not contain the ending index, that is, it does not contain the last digit.-1 indicates the index of the last position in the list.
A = [1, 2, 3, 4, 5, 6] b1 = a [:] # omitting all indicates intercepting all the content. You can copy one list to another and print (b1) it)
Result: [1, 2, 3, 4, 5, 6]
B = a [0:-] # From the position 0 to the end, increase by 1 each time and intercept. Excluding the ending index position print (B)
Result: [1, 2, 3, 4, 5]
C1 = a [: 3] # omit the index at the starting position and step size. The default start position starts from the beginning, the default step is 1, and the end position index is 3 print (c1)
Result: [1, 2, 3]
C = a [] # From the first position to the left position, take a print (c) value for each three)
Result: [1, 4]
D = a [5: 0:-1] # print (d)
Result: [6, 5, 4, 3, 2]
D1 = a [:-1] print (d1)
Result: [6, 5, 4, 3, 2, 1]