DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Tuple in Python (3)

Buy Me a Coffee

*Memos:

  • My post explains a tuple.
  • My post explains the tuple with indexing and the useful functions.
  • My post explains variable assignment.
  • My post explains shallow copy and deep copy.

You can access a tuple 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).
  • end(Optional-Default:The index of the last element - 1).
  • step(Optional-Default:1). *step mustn't be zero.
  • The [] with at least one : is slicing.
  • Slicing does shallow copy.
v1 = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') # 1D tuple

print(v1[:])
print(v1[::])
# ('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
v = (('a', 'b', 'c', 'd'), ('e', 'f', 'g', 'h')) # 2D tuple

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
v = ((('a', 'b'), ('c', 'd')), (('e', 'f'), ('g', 'h'))) # 3D tuple

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

You cannot change a tuple by indexing or slicing because it's immutable as shown below. *A del statement can still be used to remove a variable itself:

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

v[0] = 'X'
# v[-6] = '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]
# del v[-6]
del 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:
*Memos:

  • list() can create the copy of a list.
  • tuple() can create the copy of a tuple.
v = ('a', 'b', 'c', 'd', 'e', 'f')

v = list(v) # Here

v[0] = 'X'
# v[-6] = 'X'
v[2:6] = ['Y', 'Z']

v = tuple(v) # Here

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

v = list(v) # Here

del v[0]
# del v[-6]
del v[3:5]

v = tuple(v) # Here

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')
                                         # v2 = v1
                                         # v3 = v2
print(v1) # ('a', 'b', 'c', 'd', 'e')
print(v2) # ('a', 'b', 'c', 'd', 'e')
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.
  • Again, slicing does shallow copy.
  • copy() can also do shallow copy. *There are no arguments.
  • deepcopy() can do deep copy. *There are no arguments.
  • deepcopy() should be used because it's safe, doing copy deeply while copy() isn't safe, doing copy shallowly.
from copy import copy
from copy import deepcopy

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(v1)
v2 = 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

Top comments (0)