Tuple Methods in Python with Example

Unlike Python lists, tuple does not support various methods or functions such as append(), remove(), extend(), insert(), reverse(), pop(), and sort() because of its immutable nature.

However, there are many built-in functions Python tuples support them. They are as:

  • count()
  • index()
  • len()
  • max()
  • min()
  • sorted()
  • sum()
  • tuple()

Let’s discuss each method in detail one by one with the help of examples.

Python Tuple Methods


1. count(): This method returns the number of occurrences of an element appearing in a tuple. It returns 0 if the element is not present in the tuple.

The general syntax to declare count() method is:

my_tuple.count(x)

Here, my_tuple is the name of tuple and x is the number of elements to be counted. Let’s take an example based on this function.

Example 1:

# Python program to count the number of times element appears in the tuple.
# Create a tuple.
my_tuple = ("p", "r", "o", "g", "r", "a", "m", "m", "e", "r")

# Count the number of times element p.
count_p = my_tuple.count("p")
print(count_p)

# Count the number of times element m.
count_m = my_tuple.count("m")
print(count_m)

# Count the number of times element r.
count_r = my_tuple.count("r")
print(count_r)

# Count the number of times element x.
count_x = my_tuple.count("x")
print(count_x)
Output:
       1
       2
       3
       0

2. index(): This method returns the index position of the specified element at the first occurrence in the tuple. It will raise an error ValueError if the specified element in the tuple is not found.

The general syntax to declare the index() function in Python:

my_tuple.index(x)

Here, my_tuple is the name of tuple and x is the element to search for in the tuple.

Example 2:

# Python program to get index position of specified element in the tuple.
my_tuple = ("p", "r", "o", "g", "r", "a", "m", "m", "e", "r")

# Getting the index position of element o.
index_position_o = my_tuple.index("o")
print(index_position_o)

# Getting the index position of element r.
index_position_r = my_tuple.index("r")
print(index_position_r)

# Getting the index position of element m.
index_position_m = my_tuple.index("m")
print(index_position_m)
Output:
       2
       1
       6

Built-in Methods with Tuples


In Python, we can use several built-in methods or functions with tuple to carry out specific operations. Here are the methods we can use with a tuple:

1. len(): This built-in method returns the number of elements present in the tuple. The general syntax to declare len() method is as:

len(my_tuple)

Here, my_tuple is the name of tuple.

Example 3:

# Python program to determine the length of tuple.
tup1 = ("Dhanbad", "New York", "Sydney", "Cape town")
tup2 = (10, 12, 14, 16, 18)

# Find the length of tup1 and tup2.
len_tup1 = len(tup1)
len_tup2 = len(tup2)

print("Length of tup1: ", len_tup1)
print("Length of tup2: ", len_tup2)
Output:
       Length of tup1:  4
       Length of tup2:  5

2. max(): This built-in method returns the largest element of a tuple sequence. The general syntax to declare max() method is as:

max(my_tuple)

Example 4:

# Python program to find largest number in the tuple.
my_tuple = (10, 12, 14, 16, 18)

# finding the largest number.
max_num = max(my_tuple)
print("Largest number: ", max_num)
Output:
       Largest number:  18

When a tuple contains elements of purely string data type, max() evaluates the elements alphabetically and returns the last element. Look at the below example code.

Example 5:

my_tuple = ("car", "zebra", "book", "hat", "shop", "computer")
print(max(my_tuple))
Output:
       zebra

Note: Do not use the max() function on tuples with mixed data types (string and numbers) because it will generate a TypeError due to the use of unorderable types.


3. min(): This built-in function returns the smallest element of a tuple. The general syntax to define min() method is as:

min(my_tuple)

Example 6:

# Python program to find the smallest value in a tuple.
my_tuple = (2, 5, 8, 10, 1, 3)

# Finding the smallest value.
print(min(my_tuple))
Output:
       1

When a tuple contains elements of purely string data type, the min() function evaluates the elements alphabetically and returns the first element.

Example 7:

my_tuple = ('John', 'Bob', 'Ary', 'Kitty')
print(min(my_tuple))
Output:
       Ary

4. sorted(): This method returns a tuple with elements in a sorted order. The basic syntax to define sorted() method is as:

sorted(my_tuple)

Example 8:

# Python program to sort tuple elements.
my_tuple = (4, 5, 2, 3, 8, 9, 1)

# Sorting the tuple values.
print(sorted(my_tuple))
Output:
      [1, 2, 3, 4, 5, 8, 9]

Example 9:

# Python program to sort tuple elements of string data types.
my_tuple = ('Mango', 'Apple', 'Guava', 'Orange', 'Banana')

# Sorting the tuple values of type string.
print(sorted(my_tuple))
Output:
       ['Apple', 'Banana', 'Guava', 'Mango', 'Orange']

5. sum(): This method returns the total of all element values of a tuple. The basic syntax to define sum() is as:

sum(my_tuple)

Example 10:

# Python program to get the total of all elements on a tuple.
my_tuple = (2, 5, 7, 8, 10, 20, 50)

# Getting the sum of values in a tuple.
print(sum(my_tuple))
Output:
      102

6. tuple(): This built-in method is used to create an empty tuple. The general syntax to define tuple() function is as:

tuple(sequence)

Here, sequence can be a number, string or tuple itself. If the sequence is not provided, then it will create an empty tuple.

Example 11:

# Python program to create an empty tuple.
my_tuple = tuple()

# Getting the sum of values in a tuple.
print("Empty tuple: ", (my_tuple))
Output:
       Empty tuple:  ()

Example 12:

# Python program to convert a list into tuple.
my_list = [1, 2, 5, 6, 8, 10]

# Converting a list into tuple.
tup = tuple(my_list)
print("Tuple elements: ", tup)
Output:
       Tuple elements:  (1, 2, 5, 6, 8, 10)

In this tutorial, we have discussed tuple methods in Python with the help of examples. Hope that you will have understood the basic points of each tuple method and practiced example programs. In the next, we will perform the most important tuple operations in Python with the help of various examples.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love