DEV Community

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

Posted on • Edited on

Tuple in Python (2)

Buy Me a Coffee

*Memo:

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

index() can get the 1st index of the element matched to value from the tuple between [start, end), searching from the left to the right in the tuple as shown below:

*Memo:

  • The 1st argument is value(Required-Type:Any).
  • The 2nd argument is start(Optional-Default:0-Type:int):
    • It's a start index(inclusive).
    • Don't use start=.
  • The 3rd argument is end(Optional-Default:9223372036854775807-Type:int):
    • It's an end index(exclusive).
    • Don't use end=.
  • Error occurs if value doesn't exist.
v = ('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D')

print(v.index('B'))       # 1
print(v.index('B', 2))    # 5
print(v.index('B', 2, 6)) # 5
print(v.index('B', 2, 5)) # ValueError: tuple.index(x): x not in tuple
Enter fullscreen mode Exit fullscreen mode

count() can count the elements matched to value in the tuple as shown below:

*Memo:

  • The 1st argument is value(Required-Type:Any):
    • Don't use value=.
v = ('A', 'B', 'C', 'A', 'B', 'B', 'A', 'B')

print(v.count('A')) # 3
print(v.count('B')) # 4
print(v.count('C')) # 1
print(v.count('D')) # 0
print(v.count('a')) # 0
Enter fullscreen mode Exit fullscreen mode

sorted() can convert a tuple to a list, then sort the list, then the sorted list is converted to a tuple with tuple() as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is key(Optional-Default:None-Type:Callable or NoneType).
  • The 3rd argument is reverse(Optional-Default:False-Type:bool) to reverse the list.
  • sorted() creates a copy:
    • Be careful, sorted() does shallow copy instead of deep copy as my issue.
v = (3, 5, -2, 1, -4)

print(sorted(v))
print(sorted(v, key=None, reverse=False))
# [-4, -2, 1, 3, 5]

print(tuple(sorted(v)))
# (-4, -2, 1, 3, 5)

print(tuple(sorted(v, reverse=True)))
# (5, 3, 1, -2, -4)

print(tuple(sorted(v, key=abs)))
# (1, -2, 3, -4, 5)

print(tuple(sorted(v, key=abs, reverse=True)))
# (5, -4, 3, -2, 1)
Enter fullscreen mode Exit fullscreen mode
v = ("apple", "Banana", "Kiwi", "cherry")

""" Case sensitive sort """
print(tuple(sorted(v)))
# ('Banana', 'Kiwi', 'apple', 'cherry')

""" Case insensitive sort """
print(tuple(sorted(v, key=str.upper)))
print(tuple(sorted(v, key=str.lower)))
# ('apple', 'Banana', 'cherry', 'Kiwi')

""" Sort by the length of a word """
print(tuple(sorted(v, key=len)))
# ('Kiwi', 'apple', 'Banana', 'cherry')
Enter fullscreen mode Exit fullscreen mode

reversed() can return the iterator which has the reversed elements of a tuple, then the iterator is converted to a tuple with tuple() as shown below:

*Memo:

  • The 1st argument is seq(Required-Type:Sequence):
    • Don't use seq=.
v = (3, 5, -2, 1, -4)

print(reversed(v))
# <reversed object at 0x000001F3B9D83C10>

print(tuple(reversed(v)))
# (-4, 1, -2, 5, 3)
Enter fullscreen mode Exit fullscreen mode
v = ("apple", "Banana", "Kiwi", "cherry")

print(tuple(reversed(v)))
# ('cherry', 'Kiwi', 'Banana', 'apple')
Enter fullscreen mode Exit fullscreen mode

A tuple can be read by indexing as shown below. *Indexing can be done with one or more [index]:

1D tuple:

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

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# A B C D E F G H
Enter fullscreen mode Exit fullscreen mode

2D tuple:

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

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

print(v[0][0], v[0][1], v[0][2], v[0][3], v[1][0], v[1][1], v[1][2], v[1][3])
print(v[-2][-4], v[-2][-3], v[-2][-2], v[-2][-1],
      v[-1][-4], v[-1][-3], v[-1][-2], v[-1][-1])
# A B C D E F G H
Enter fullscreen mode Exit fullscreen mode

3D tuple:

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

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

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

print(v[0][0][0], v[0][0][1], v[0][1][0], v[0][1][1], v[1][0][0],
      v[1][0][1], v[1][1][0], v[1][1][1])
print(v[-2][-2][-2], v[-2][-2][-1], v[-2][-1][-2], v[-2][-1][-1],
      v[-1][-2][-2], v[-1][-2][-1], v[-1][-1][-2], v[-1][-1][-1])
# A B C D E F G H
Enter fullscreen mode Exit fullscreen mode

Top comments (0)