Operations on Tuple Data Structure in Python

This post is lesson 25 of 54 in the subject Python Programming Language

In the article Tuple Data Structure in Python, you learned about Tuples. This article will explore some operations on Tuple Data Structure in Python.

1. How to Unpack a Tuple in Python?

Python allows us to extract elements in Tuple to assign to variables. This is called unpacking. Example:

my_tuple = ('hello', 1.5, 'Gochocit.com')
(item1, item2, item3) = my_tuple
print(item1)
print(item2)
print(item3)

Result

hello
1.5
Gochocit.com

If the number of variables is less than the number of elements in the Tuple, we can add the asterisk (*) to the variable name. At this point, the elements will be assigned to the variable as a list:

my_tuple = ('hello', 1.5, 9, 'Gochocit.com', 10)
# Asterisk of last variable
(item1, item2, *item3) = my_tuple
print("Asterisk of last variable:")
print(item1)
print(item2)
print(item3)
# Asterisk of second variable
(item1, *item2, item3) = my_tuple
print("Asterisk of second variable:")
print(item1)
print(item2)
print(item3)

Result

Asterisk of last variable:
hello
1.5
[9, 'Gochocit.com', 10]
Asterisk of second variable:
hello
[1.5, 9, 'Gochocit.com']
10

2. Traverse each element in Tuple

2.1. Using for loop

my_tuple = ('hello', 1.5, 9, 'Gochocit.com', 10)
for x in my_tuple:
    print(x, end=' ')

Result

hello 1.5 9 Gochocit.com 10

2.2. Using for loop with index

We can traverse the elements in Tuple by determining their index.

my_tuple = ('hello', 1.5, 9, 'Gochocit.com', 10)
for i in range(len(my_tuple)):
    print(my_tuple[i], end=' ')

Result

hello 1.5 9 Gochocit.com 10

2.3. Using a while loop

my_tuple = ('hello', 1.5, 9, 'Gochocit.com', 10)
i = 0
while i < len(my_tuple):
  print(my_tuple[i], end=' ')
  i = i + 1

Result

hello 1.5 9 Gochocit.com 10

3. Concatenate Tuples

We can use the + operator to concatenate tuples or the * operator to repeat elements in a tuple with a specific number of times.

my_tuple1 = ("hello", 1, 9)
my_tuple2 = (1.5 , "Gochocit.com")
# Using operator + with tuples
my_tuple = my_tuple1 + my_tuple2
print("Using operator + with tuples:", my_tuple)
# Repeat tuple with operator *
repeat_my_tuple1 = my_tuple1 * 3
print("Repeat my_tuple1:", repeat_my_tuple1)

Result

Using operator + with tuples: ('hello', 1, 9, 1.5, 'Gochocit.com')
Repeat my_tuple1: ('hello', 1, 9, 'hello', 1, 9, 'hello', 1, 9)

4. Tuple Methods in Python

In Python, Tuple is a data structure that cannot change its elements. There are no methods to add or remove elements with Tuple. Python only supports the 2 methods below for Tuple.

MethodFunction
count()Returns the number of times an element appears in the Tuple
index()Searches for an element in the Tuple and returns the index where the element is found in Tuple

Example using Tuple methods

my_tuple = ('hello', 1.5, 9, 'Gochocit.com', 10, 'hello')
# Output: 2
print(my_tuple.count('hello'))
# Output: 3
print(my_tuple.index('Gochocit.com'))

5. Advantages of Tuple

– Tuples are commonly used to store elements with different data types.

– Traversing through elements in a Tuple is faster than in a List.

– You can use the elements in a Tuple as a key and assign a value to a Dictionary data structure.

– If the data to be stored does not change, use a Tuple to store it.

5/5 - (1 vote)
Previous and next lesson in subject<< The Tuple Data Structure in PythonSet data structure in Python >>

Leave a Reply

Your email address will not be published. Required fields are marked *