Tuple in Python (3)
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 (3)

Publish Date: Jun 1
0 0

Buy Me a Coffee

*Memos:

  • My post explains a tuple.
  • My post explains tuple functions and the tuple with indexing.
  • My post explains the shallow copy and deep copy of a tuple.

A tuple can be read by slicing as shown below:

*Memos:

  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element):
      • It's a start index(inclusive).
    • end(Optional-Default:The index of the last element + 1):
      • 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.
1D tuple:
v1 = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')

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

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

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

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

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

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

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

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

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

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

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

print(v1[2:6:-2])
print(v1[-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 or slicing as shown below. *A del statement can still be used to remove one or more variables themselves:

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

v[0] = 'X'
v[2:6] = ['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[0], v[3: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[0] = 'X'
v[2:6] = ['Y', 'Z']

v = tuple(v)

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

v = list(v)

del v[0], v[3:5]

v = tuple(v)

print(v)
# ('b', '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

The variables v1 and v2 always refer to the same tuple because a tuple cannot be copied as shown below:

*Memos:

  • is keyword can check if v1 and v2 refer to the same tuple.
  • copy.copy() and slicing do shallow copy.
  • tuple() doesn't do shallow copy.
  • copy.deepcopy() does deep copy.
  • copy.deepcopy() should be used because it's safe, doing copy deeply while copy.copy() and slicing aren't safe, doing copy shallowly.
from copy import copy

v1 = ('a', 'b', 'c', 'd', 'e')

v2 = v1 # v2 refers to the same tuple as v1.

print(v1) # ('a', 'b', 'c', 'd', 'e')
print(v2) # ('a', 'b', 'c', 'd', 'e')

print(v1 is v2)
# True

v2 = v1[:] # v2 refers to the same tuple as v1.
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)

print(v1) # ('a', 'b', 'c', 'd', 'e')
print(v2) # ('a', 'b', 'c', 'd', 'e')

print(v1 is v2)
# True
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment