sec02 - tuple Slicing

Tuple slicing works similarly to list slicing. Slicing extracts a specified range of elements to create a new tuple, so it can be used even with tuples that do not allow adding or editing elements.

Table of Contents(目次)

スポンサーリンク

Basic Tuple Indexes

This lecture will use the following tuple, which contains multiple data types, as an example.

sample_tuple = ('zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6, (7,))
[0][1][2][3][4][5][6][7]
[-8][-7][-6][-5][-4][-3][-2][-1]
'zero'1-222-3.3[4, 4.4]"5-five"6.6(7,)

First, let’s review how to specify basic indexes.

The first index is [0], and the numbers increase sequentially. The sample tuple contains 8 elements, so indexes from [0] through [7] are assigned.

You can also specify indexes counting backward from the end. In that case, you start with [-1] and count backward.

sample_tuple = ('zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6, (7,))

# From index [0] in order
print(sample_tuple[0])  # 'zero'
print(sample_tuple[1])  # 1
print(sample_tuple[2])  # -222
print(sample_tuple[3])  # -3.3
print(sample_tuple[4])  # [4, 4.4]
print(sample_tuple[5])  # "5-five"
print(sample_tuple[6])  # 6.6
print(sample_tuple[7])  # (7,)

# From index [-1] in reverse order
print(sample_tuple[-1])  # (7,)
print(sample_tuple[-2])  # 6.6
print(sample_tuple[-3])  # "5-five"
print(sample_tuple[-4])  # [4, 4.4]
print(sample_tuple[-5])  # -3.3
print(sample_tuple[-6])  # -222
print(sample_tuple[-7])  # 1
print(sample_tuple[-8])  # 'zero'

Tuple Slicing

The rules for tuple slicing are as follows. They are basically the same as for strings.

  • [n:m]: Returns a tuple from [n] up to one element before [m]
    • The rule is the same even if you specify indexes as negative numbers.
    • The range specified by n and m can go beyond the actual index range of the tuple.
      (※ When specifying a single element ([n]), the index must be within the valid range.)
  • [:m]: If the starting element is omitted, returns a tuple from [0] up to one element before [m]
  • [n:]: If the ending element is omitted, returns a tuple from [n] to the last element
  • [:]: If nothing is specified on either side of the colon, returns a tuple from [0] to the last element
  • [n:m:step]: By providing a step value, extracts elements at intervals within the range [n:m]
    • When using step, the n and m values can be omitted.
    • You can also specify a negative value for step.
      • ※ If a negative value is specified, elements are extracted in reverse order. In this case, make sure the position of m is to the left of n; otherwise, an empty tuple will be returned. (Specify the starting and ending indexes in the order you want elements extracted.)
        Example: specify it like [5:1:-1].

The code below serves as a sample for each rule. Let's review the output results and compare them with the element number table.

sample_tuple = ('zero', 1, -222, -3.3, [4, 4.4], "5-five", 6.6, (7,))

print('[0:3]:\t',    sample_tuple[0:3])     # [0:3]:    ('zero', 1, -222)
print('[4:-1]:\t',   sample_tuple[4:-1])    # [4:-1]:   ([4, 4.4], '5-five', 6.6)
print('[-9:9]:\t',   sample_tuple[-9:9])    # [-9:9]:   ('zero', 1, -222, -3.3, [4, 4.4], '5-five', 6.6, (7,))
print('[:6]:\t',     sample_tuple[:6])      # [:6]:     ('zero', 1, -222, -3.3, [4, 4.4], '5-five')
print('[-4:]:\t',    sample_tuple[-4:])     # [-4:]:    ([4, 4.4], '5-five', 6.6, (7,))
print('[:]:\t',      sample_tuple[:])       # [:]:      ('zero', 1, -222, -3.3, [4, 4.4], '5-five', 6.6, (7,))
print('[0:6:2]:\t',  sample_tuple[0:6:2])   # [0:6:2]:  ('zero', -222, [4, 4.4])
print('[::-1]:\t',   sample_tuple[::-1])    # [::-1]:   ((7,), 6.6, '5-five', [4, 4.4], -3.3, -222, 1, 'zero')
print('[5:1:-1]:\t', sample_tuple[5:1:-1])  # [5:1:-1]: ('5-five', [4, 4.4], -3.3, -222)
print('[1:5:-1]:\t', sample_tuple[1:5:-1])  # [1:5:-1]: ()  * Since 'n' and 'm' are reversed, an empty tuple is returned
[0][1][2][3][4][5][6][7]
[-8][-7][-6][-5][-4][-3][-2][-1]
'zero'1-222-3.3[4, 4.4]"5-five"6.6(7,)
スポンサーリンク