DEV Community

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

Posted on • Edited on

Tuple in Python (2)

Buy Me a Coffee

*Memos:

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

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

v = () # 1D tuple

print(v) # ()

v = ('a',) # 1D tuple
v = 'a',   # 1D tuple
v = (('a',)) # 1D tuple

print(v[0])
print(v[-1])
# a

v = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') # 1D tuple
v = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' # 1D tuple

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

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

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

You can use index() to get the index of an element from a tuple as shown below:
*Memos:

  • The 1st argument is x(Required) for an index.
  • The 2nd argument is start(Optional) for the start of an index.
  • The 3rd argument is end(Optional) for the end of an index.
  • If the element doesn't exist, there is error.
  • Don't use x=, start= and end=.
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

You can use count() to count how many selected elements there are in a tuple as shown below:
*Memos:

  • The 1st argument is x(Required) for an element.
  • *Don't use x=.
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'))
print(v.count('A'))
# 0
Enter fullscreen mode Exit fullscreen mode

You can use sorted() to sort a tuple as shown below. Finally, a list is returned:
*Memos:

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

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

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

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

print(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(sorted(v))
# ['Banana', 'Kiwi', 'apple', 'cherry']

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

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

Top comments (0)