
The concept of slicing a list is the same as slicing a string. In the case of strings, each character is processed individually, but in the case of lists, each element is processed individually.
Table of Contents(目次)
Basics of list indexes
In this lecture, we will use the following list containing multiple data types for explanation.
sample_list = ['zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6]
[0] | [1] | [2] | [3] | [4] | [5] | [6] |
[-7] | [-6] | [-5] | [-4] | [-3] | [-2] | [-1] |
'zero' | 1 | -222 | -3.3 | [4, 4.4] | "5-five" | 6.6 |
First, let's review the basics of index specification.
The first index is [0]
, and the numbers are assigned in order. In this sample, there are seven elements, so the indexes [0]
to [6]
are assigned.
You can also specify the indexes in reverse order. In that case, specify the numbers in order, starting from [-1]
.
sample_list = ['zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6]
# In order from index [0]
print(sample_list[0]) # 'zero'
print(sample_list[1]) # 1
print(sample_list[2]) # -222
print(sample_list[3]) # -3.3
print(sample_list[4]) # [4, 4.4]
print(sample_list[5]) # "5-five"
print(sample_list[6]) # 6.6
# In order from index [-1]
print(sample_list[-1]) # 6.6
print(sample_list[-2]) # "5-five"
print(sample_list[-3]) # [4, 4.4]
print(sample_list[-4]) # -3.3
print(sample_list[-5]) # -222
print(sample_list[-6]) # 1
print(sample_list[-7]) # 'zero'
Slicing lists
The rules for slicing lists are as follows. They are basically the same as for strings.
[n:m]
: Returns a list from [n] to one before [m].- The same rule applies when specifying indexes with negative values.
- The range specified by n and m can be outside the actual list index range.
(※ When specifying a single element ([n]
), specify it within the index range.)
[:m]
: If the first element is not specified, returns a list from [0] to one before [m].[n:]
: If the last element is not specified, returns a list from [n] to the last element.[:]
: If nothing is specified on either side of the colon, it returns a list from [0] to the last element.[n:m:step]
: If you enter the number of steps, it extracts elements within the range [n:m] at intervals of the specified number of steps.- When using step, the values of n and m can be omitted.
- You can also specify a negative value for step.
- * If a negative value is specified, the elements are extracted in reverse order. In this case, note that if the value of [n] is not specified to the left of the value of [m], a list of element zero will be returned. (Specify the start index and end index in the order in which the elements are to be extracted.)
Example: Specify the start and end points as[5:1:-1]
.
- * If a negative value is specified, the elements are extracted in reverse order. In this case, note that if the value of [n] is not specified to the left of the value of [m], a list of element zero will be returned. (Specify the start index and end index in the order in which the elements are to be extracted.)
The following code is a sample of each rule. Check the output results against the index table.
sample_list = ['zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6]
print('[0:3]:\t', sample_list[0:3]) # [0:3]: ['zero', 1, -222]
print('[4:-1]:\t', sample_list[4:-1]) # [4:-1]: [[4, 4.4], '5-five']
print('[-9:9]:\t', sample_list[-9:9]) # [-9:9]: ['zero', 1, -222, -3.3, [4, 4.4], '5-five', 6.6]
print('[:6]:\t', sample_list[:6]) # [:6]: ['zero', 1, -222, -3.3, [4, 4.4], '5-five']
print('[-4:]:\t', sample_list[-4:]) # [-4:]: [-3.3, [4, 4.4], '5-five', 6.6]
print('[:]:\t', sample_list[:]) # [:]: ['zero', 1, -222, -3.3, [4, 4.4], '5-five', 6.6]
print('[0:6:2]:\t', sample_list[0:6:2]) # [0:6:2]: ['zero', -222, [4, 4.4]]
print('[::-1]:\t', sample_list[::-1]) # [::-1]: [6.6, '5-five', [4, 4.4], -3.3, -222, 1, 'zero']
print('[5:1:-1]:\t', sample_list[5:1:-1]) # [5:1:-1]: ['5-five', [4, 4.4], -3.3, -222]
print('[1:5:-1]:\t', sample_list[1:5:-1]) # [1:5:-1]: [] (* Since n and m are specified in reverse order, a list with zero elements is returned.)
[0] | [1] | [2] | [3] | [4] | [5] | [6] |
[-7] | [-6] | [-5] | [-4] | [-3] | [-2] | [-1] |
'zero' | 1 | -222 | -3.3 | [4, 4.4] | "5-five" | 6.6 |