Tuple in Python (4)
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

Tuple in Python (4)

Publish Date: Oct 7
0 0

Buy Me a Coffee

*Memo:

A tuple can be read by slicing as shown below:

*Memo:

  • Slicing can be done with one or more [start:end:step] in the range [start, end):
    • start(Optional-Default:The 1st element index):
      • It's a start index(inclusive).
    • end(Optional-Default:The next index to the last element index):
      • It's an end index(exclusive).
    • step(Optional-Default:1):
      • It's the interval of indices.
      • It cannot be zero.
    • The [] with at least one : is slicing.
    • Error doesn't occur even if [start, end) is out of the range [The 1st element index, The last element index].
    • Signed indices(zeros, positive and negative indices) are possible.
  • Indexing and slicing can be used together.

<1D tuple>:

v = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')

print(v[:])
print(v[::])
print(v[0:8:1])
print(v[-100:100:1])
# ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')

print(v[::2])
# ('A', 'C', 'E', 'G')

print(v[::-2])
# ('H', 'F', 'D', 'B')

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# ('C', 'D', 'E', 'F', 'G', 'H')

print(v[2::2])
print(v[-6::2])
# ('C', 'E', 'G')

print(v[2::-2])
print(v[-6::-2])
# ('C', 'A')

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# ('A', 'B', 'C', 'D', 'E', 'F')

print(v[:6:2])
print(v[:-2:2])
# ('A', 'C', 'E')

print(v[:6:-2])
print(v[:-2:-2])
# ('H',)

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# ('C', 'D', 'E', 'F')

print(v[2:6:2])
print(v[-6:-2:2])
# ('C', 'E')

print(v[2:6:-2])
print(v[-6:-2:-2])
# ()
Enter fullscreen mode Exit fullscreen mode

<2D tuple>:

v = (('A', 'B', 'C', 'D'), ('E', 'F', 'G', 'H'))

print(v[:])
print(v[::])
print(v[:][:])
print(v[::][::])
# (('A', 'B', 'C', 'D'), ('E', 'F', 'G', 'H'))

print(v[0][:])
print(v[0][::])
print(v[-2][:])
print(v[-2][::])
# ('A', 'B', 'C', 'D')

print(v[0][::2])
print(v[-2][::2])
# ('A', 'C')

print(v[0][::-2])
print(v[-2][::-2])
# ('D', 'B')

print(v[1][:])
print(v[1][::])
print(v[-1][:])
print(v[-1][::])
# ('E', 'F', 'G', 'H')

print(v[1][::2])
print(v[-1][::2])
# ('E', 'G')

print(v[1][::-2])
print(v[-1][::-2])
# ('H', 'F')
Enter fullscreen mode Exit fullscreen mode

<3D tuple>:

v = ((('A', 'B'), ('C', 'D')), (('E', 'F'), ('G', 'H')))

print(v[:])
print(v[::])
print(v[:][:])
print(v[::][::])
print(v[:][:][:])
print(v[::][::][::])
# ((('A', 'B'), ('C', 'D')), (('E', 'F'), ('G', 'H')))

print(v[0][:])
print(v[0][::])
print(v[-2][:])
print(v[-2][::])
# (('A', 'B'), ('C', 'D'))

print(v[1][:])
print(v[1][::])
print(v[-1][:])
print(v[-1][::])
# (('E', 'F'), ('G', 'H'))

print(v[0][0][:])
print(v[0][0][::])
print(v[-2][-2][:])
print(v[-2][-2][::])
# ('A', 'B')

print(v[0][0][::2])
print(v[-2][-2][::2])
# ('A',)

print(v[0][0][::-2])
print(v[-2][-2][::-2])
# ('B',)

print(v[0][1][:])
print(v[0][1][::])
print(v[-2][-1][:])
print(v[-2][-1][::])
# ('C', 'D')

print(v[0][1][::2])
print(v[-2][-1][::2])
# ('C',)

print(v[0][1][::-2])
print(v[-2][-1][::-2])
# ('D',)

print(v[1][0][:])
print(v[1][0][::])
print(v[-1][-2][:])
print(v[-1][-2][::])
# ('E', 'F')

print(v[1][0][::2])
print(v[-1][-2][::2])
# ('E',)

print(v[1][0][::-2])
print(v[-1][-2][::-2])
# ('F',)

print(v[1][1][:])
print(v[1][1][::])
print(v[-1][-1][:])
print(v[-1][-1][::])
# ('G', 'H')

print(v[1][1][::2])
print(v[-1][-1][::2])
# ('G',)

print(v[1][1][::-2])
print(v[-1][-1][::-2])
# ('H',)
Enter fullscreen mode Exit fullscreen mode

A tuple cannot be changed by indexing, slicing and a del statement as shown below:

*Memo:

  • A del statement cannot remove zero or more elements from a tuple by indexing and slicing but can remove one or more variables themselves.
v = ('a', 'b', 'c', 'd', 'e', 'f')

v[1] = 'X'
v[-5] = 'X'
v[3:5] = ['Y', 'Z']
v[-3:-1] = ['Y', 'Z']
v[1], v[3:5] = 'X', ['Y', 'Z']
v[-5], v[-3:-1] = 'X', ['Y', 'Z']
# TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = ('a', 'b', 'c', 'd', 'e', 'f')

del v[1], v[3:5]
# del v[-5], v[-2:5]
# TypeError: 'tuple' object does not support item deletion
Enter fullscreen mode Exit fullscreen mode
v = ('a', 'b', 'c', 'd', 'e', 'f')

del v

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

If you really want to change a tuple, use list() and tuple() as shown below:

v = ('a', 'b', 'c', 'd', 'e', 'f')

v = list(v)

v[1] = 'X'
v[-5] = 'X'
v[3:5] = ['Y', 'Z']
v[-3:-1] = ['Y', 'Z']

v[1], v[3:5] = 'X', ['Y', 'Z']
v[-5], v[-3:-1] = 'X', ['Y', 'Z']

v = tuple(v)

print(v)
# ('a', 'X', 'c', 'Y', 'Z', 'f')
Enter fullscreen mode Exit fullscreen mode
v = ('a', 'b', 'c', 'd', 'e', 'f')

v = list(v)

del v[1], v[3:5]
# del v[-5], v[-2:5]

v = tuple(v)

print(v)
# ('a', 'c', 'd')
Enter fullscreen mode Exit fullscreen mode

A tuple can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = ('A', 'B', 'C', 'D', 'E') # Equivalent
                                         # v1 = ('A', 'B', 'C', 'D', 'E')
print(v1) # ('A', 'B', 'C', 'D', 'E')    # v2 = v1
print(v2) # ('A', 'B', 'C', 'D', 'E')    # v3 = v2
print(v3) # ('A', 'B', 'C', 'D', 'E')
Enter fullscreen mode Exit fullscreen mode

A tuple cannot be shallow-copied and deep-copied as shown below:

<Shallow & Deep copy>:

*Memo:

  • v1 and v2 refer to the same outer and inner tuple.
  • is keyword can check if v1 and v2 refer to the same outer and inner tuple.
  • copy.copy(), tuple() and slicing cannot shallow-copy a tuple.
  • copy.deepcopy() cannot deep-copy and even shallow-copy a tuple.
import copy

v1 = ('A', 'B', ('C', 'D'))
v2 = copy.copy(v1)
v2 = tuple(v1)
v2 = v1[:]
v2 = copy.deepcopy(v1)

print(v1) # ('A', 'B', ('C', 'D'))
print(v2) # ('A', 'B', ('C', 'D'))

print(v1 is v2, v1[2] is v2[2])
# True True
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment