DEV Community

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

Posted on

Tuple in Python (5)

Buy Me a Coffee

*Memo:

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/or 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

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

Top comments (0)