DEV Community

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

Posted on • Edited on

Tuple functions in Python

Buy Me a Coffee

*Memo:

index() can get the 1st index of the element matched to value from the tuple in the range [start, end) 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.
  • start and end can be signed indices(zero and positive and negative indices).
  • Error doesn't occur even if [start, end) is out of the range [The 1st index, The tuple length).
v = ('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D')

print(v.index('B'))                                            # 1
print(v.index('B', 0, 9223372036854775807))                    # 1
print(v.index('B', -9223372036854775808, -1))                  # 1
print(v.index('B', -9223372036854775808, 9223372036854775807)) # 1

print(v.index('B', 2))                                         # 5
print(v.index('B', -6))                                        # 5
print(v.index('B', 2, 6))                                      # 5
print(v.index('B', -6, -2))                                    # 5

print(v.index('B', 9223372036854775807, -9223372036854775808))
print(v.index('B', 2, 5))
print(v.index('B', 6, -3))
# 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/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)