The Tuple Data Structure in Python

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

1. Characteristics of Tuple data structure in Python

Tuple data structure in Python stores multiple elements like List. But the elements in Tuple have order and cannot be changed. The elements of Tuple are placed in parentheses (round brackets), can be any data type, and are separated by commas. Example:

# Empty tuple
my_tuple = ()
print("Empty tuple:", my_tuple)

# Tuple having integers
my_tuple = (1, 2, 3)
print("Tuple having integers:", my_tuple)

# Tuple with mixed datatypes
my_tuple = (1, "Gochocit", 3.4)
print("Tuple with mixed datatypes:", my_tuple)

# Nested tuple
my_tuple = ("Gochocit", [8, 4, 6], (1, 2, 3))
print("Nested tuple:", my_tuple)

Result

Empty tuple: ()
Tuple having integers: (1, 2, 3)
Tuple with mixed datatypes: (1, 'Gochocit', 3.4)
Nested tuple: ('Gochocit', [8, 4, 6], (1, 2, 3))

Note: If you create a tuple with one element, a comma at the end is required. Without a comma, Python will not recognize it as a tuple.

my_tuple = ("hello")
print("my_tuple is a string:", type(my_tuple))
my_tuple = ("hello",)
print("my_tuple is a tuple:", type(my_tuple))

Result

my_tuple is a string: <class 'str'>
my_tuple is a tuple: <class 'tuple'>

Python allows multiple elements to have the same value in a Tuple.

# Duplicate items in tuple
my_tuple = ("hello", 1, 9, 1.5, 9, "hello", "Gochocit.com")
print("Duplicate items in tuple:", my_tuple)

Result

Duplicate items in tuple: ('hello', 1, 9, 1.5, 9, 'hello', 'Gochocit.com')

Size of Tuple in Python

The len() function helps determine the number of elements in a Tuple.

my_tuple = ("hello", 1, 9, 1.5, 9, "hello", "Gochocit.com")
print("Size of my_tuple:", len(my_tuple))

Result

Size of my_tuple: 7

2. Accessing elements in Tuple

The elements in Tuple are indexed. The first element has an index of 0, the second element has an index of 1, etc. The index can also be a negative integer. The last element can be indexed as -1, the second last element is -2, … Example:

my_tuple = ("hello", 1, 9, 1.5, 9, "hello", "Gochocit.com")
# Output: 1.5
print("Item at index 3:", my_tuple[3])
# Output: 9
print("Item at index -5:", my_tuple[-5])

Result

Item at index 3: 1.5
Item at index -5: 9

We can also access elements in Tuple by specifying a range of the index. Example:

my_tuple = ("hello", 1, 9, 1.5, 9, "hello", "Gochocit.com")
# Access items from 1 to 5
# Output: (1, 9, 1.5, 9, 'hello')
print("Index from 1 to 5:", my_tuple[1: 6])
# Access items from 0 to 4
# Output: ('hello', 1, 9, 1.5, 9)
print("Index from 0 to 4:", my_tuple[: 5])
# Access items from 2 to end
# Output: (9, 1.5, 9, 'hello', 'Gochocit.com')
print("Index from 2 to end:", my_tuple[2: ])

Result

Index from 1 to 5: (1, 9, 1.5, 9, 'hello')
Index from 0 to 4: ('hello', 1, 9, 1.5, 9)
Index from 2 to end: (9, 1.5, 9, 'hello', 'Gochocit.com')

Check if an item is in Tuple or not

my_tuple = ("hello", 1, 9, 1.5, 9, "hello", "Gochocit.com")
if "Gochocit.com" in my_tuple:
  print("'Gochocit.com' is in my_tuple")

Result

'Gochocit.com' is in my_tuple

3. Change elements in Tuple

We cannot change the elements in Tuple. But if the elements in the Tuple are a mutable data structure that can be changed like List, then we can change that List. In addition, we can also reassign another Tuple to a variable.

my_tuple = ("hello", 50, [1, 9, 1.5, 9] , "Gochocit.com")
# TypeError: 'tuple' object does not support item assignment
# my_tuple[1]= 'change'

# item of mutable element in Tuple can be changed
my_tuple[2][0] = 1111
print("Change item of mutable element in tuple:", my_tuple)

# Tuple can be reassigned
my_tuple = ("Gochocit.com", 1, 9, 0, 1.1)
print("Reassign my_tuple:", my_tuple)

Result

Change item of mutable element in tuple: ('hello', 50, [1111, 9, 1.5, 9], 'Gochocit.com')
Reassign my_tuple: ('Gochocit.com', 1, 9, 0, 1.1)

3.1. Add item to Tuple

Although the elements in Tuple cannot change in value or quantity, we have some ways to add items to Tuple.

Method 1 – Convert Tuple to List

my_tuple = ("hello", 1, 9, 1.5 , "Gochocit.com")
my_list = list(my_tuple)
my_list.append("welcome")
my_tuple = tuple(my_list)
print("Add item to my_tuple:", my_tuple)

Result

Add item to my_tuple: ('hello', 1, 9, 1.5, 'Gochocit.com', 'welcome')

Method 2 – Using the + and * operators

We can use the + operator to concatenate tuples or the * operator to repeat elements in a tuple with a predetermined 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)

3.2. Remove the item from Tuple

We can remove an item from the Tuple by converting the Tuple to a List. Or use the keyword del to remove the entire Tuple.

my_tuple = ('hello', 1, 9, 1.5, 'Gochocit.com')

# Remove item by converting to list
my_list = list(my_tuple)
my_list.remove("hello")
my_tuple = tuple(my_list)
print("Remove item in tuple by converting to list:", my_tuple)

# Delete my_tuple
del my_tuple
# NameError: name 'my_tuple' is not defined
# print(my_tuple)

Result

Remove item in tuple by converting to list: (1, 9, 1.5, 'Gochocit.com')
5/5 - (1 vote)
Previous and next lesson in subject<< Operations on the List Data Structure in PythonOperations on Tuple Data Structure in Python >>

Leave a Reply

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